在打印一个map容器speakRecord(第一个int记录比赛的届数,后面的vector记录了每届比赛前三名的编号,姓名和成绩)的过程中,用迭代器可以正常的打印vector容器中的字符串.
for (map<int,vector<string>>::iterator it = speakerRecord.begin(); it != this->speakerRecord.end(); it++)
{
cout << "第" <<it->first << "届比赛:"
<< "冠军编号:" << it->second[0]<< " 姓名:" << it->second[1] << " 分数:" << it->second[2]
<< " 亚军编号:" << it->second[3] << " 姓名:" << it->second[4] << " 分数:" << it->second[5]
<< " 季军编号:" << it->second[6] << " 姓名:" << it->second[7] << " 分数:" << it->second[8];
cout << endl;
}
但是用下面的方法,在运行时便会说我数组下标越界了,我看的一个视频里就是用的这种下标的方法,但是他可以正常显示,请问我的错误在哪里的呢
这是视频里的写法
这是我的写法,但是运行时会报错
for(int i = 0; i < this->speakerRecord.size(); i++)
{
cout << "第" << i + 1 << "届 " <<
<< "冠军编号:" << this->speakerRecord[i][0]<< " 姓名:" << this->speakerRecord[i][1] << " 分数:" << this->speakerRecord[i][2]
<< " 亚军编号:" << this->speakerRecord[i][3] << " 姓名:" << this->speakerRecord[i][4]<< " 分数:" < this->speakerRecord[i][5]
<< " 季军编号:" << this->speakerRecord[i][6] << " 姓名:" << this->speakerRecord[i][7] << " 分数:" << this->speakerRecord[i][8];
}
另外,我还想问一下speakerRecord[i][1] 的写法是什么意思,speakRecord[i]是一个对组,那为什么后面再跟一个下标就可以表示后面的vector的下标呢