0

Why do I get a "vector subscript out of range" error for my code?

class CSVRow
{
public:
    string const& operator[](size_t index) const
    {
        return m_data[index];
    }
    size_t size() const
    {
        return m_data.size();
    }
    void readNextRow(istream& str)
    {
        string line;
        getline(str, line);

        stringstream lineStream(line);
        string cell;

        m_data.clear();
        while (getline(lineStream, cell, ','))
        {
            m_data.push_back(cell);
        }
    }
private:
    vector<string> m_data;
}
;

istream& operator>>(istream& str, CSVRow& data)
{
    data.readNextRow(str);
    return str;

}
int main()
{
    ifstream file("full_training_dataset.csv");

    CSVRow row;
    while (file >> row)
        cout << row[1] << endl;


    return 0;
}
Sga
  • 3,608
  • 2
  • 36
  • 47
Alsphere
  • 513
  • 1
  • 7
  • 22
  • What is the structure of the CSV file you are loading? – Michiel Pater Dec 03 '15 at 10:45
  • After reading the first row, there is ust one string in the vector, whose index is 0. Accessing item 1 of that vector via your operator `[]` is an access out of bounds. – M Oehm Dec 03 '15 at 10:46
  • Actuality, I want to read the second column of my full_training_dataset.csv so that I gave row[1] not row[0] my csv file contain two columns of strings – Alsphere Dec 03 '15 at 10:56
  • You should test the `size()` before accessing a cell with a specific index. – M Oehm Dec 03 '15 at 11:00
  • *Why do I get a "vector subscript out of range" error for my code?* -- Because maybe...you're accessing the vector with an out of range subscript? What else could be the reason? The runtime is telling the truth, so it's your job to debug the code. Also, why do you assume there is a `row[1]` to access? – PaulMcKenzie Dec 03 '15 at 11:04
  • @ PaulMcKenzie coz I am trying to read the second column of my csv string data – Alsphere Dec 03 '15 at 11:09
  • @Ahmed There is no "second column" when the program is run. There is a "second column" when you look at the file with human eyes, but your program has a bug that doesn't follow what you are seeing. So again, the reason for the error is exactly as stated -- your vector subscript is out of range. – PaulMcKenzie Dec 03 '15 at 11:13
  • @PaulMcKenzie so what can do to make my vector in my range?? – Alsphere Dec 03 '15 at 20:30
  • @Ahmed You make it "in range" by fixing your program. You are *not* reading in the data correctly, and you need to fix it. No one knows what you're reading in until we see the data. Also, it doesn't matter -- your code should always make sure that the index will be in bounds -- what if next time, you have a file that has just 1 column? Your goal should be to make the program as bulletproof as possible, not make assumptions on the data. – PaulMcKenzie Dec 03 '15 at 20:33
  • @PaulMcKenzie, first column of my file has lists of "positive or negative" words and the second column has a tweet commands, I have 21604 rowes of those data and two column – Alsphere Dec 03 '15 at 20:43
  • @Ahmed A description is *not good enough*. Your code is not reading the file correctly. You could have a million columns, if the code has a bug that can't read past the first, what good is a description? – PaulMcKenzie Dec 03 '15 at 21:25

1 Answers1

1
while (file >> row)
    cout << row[1] << endl;

First time element would be inserted ar row[0] not at row[1].
ravi
  • 10,994
  • 1
  • 18
  • 36
  • Actuality, I want to read the second column of my full_training_dataset.csv so that I gave row[1] not row[0] – Alsphere Dec 03 '15 at 10:53