为什么我的代码出现"vector subscript out of range"错误?

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

本文关键字:out of range 错误 subscript vector 我的 代码 为什么      更新时间:2023-10-16

为什么我的代码会出现"矢量下标超出范围"错误?

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;
}
while (file >> row)
    cout << row[1] << endl;
First time element would be inserted ar row[0] not at row[1].