我试图在 15 行输出后暂停列出四个 vector 的元素,到目前为止它做了我想要的,但它在函数结束时崩溃了。
void List(vector<string> itemNumb, vector<string> itemName, vector<double> itemCost, vector<int> itemQuant, int length, int index)
{
index = 0;
length = itemNumb.size();
while (index <= length)
{
cout << "======================" << endl;
cout << itemNumb[index] << endl;
cout << itemName[index] << endl;
cout << itemCost[index] << endl;
cout << itemQuant[index] << endl;
if (index % 15 == 14)
{
system("pause");
system("cls");
}
index++;
}
}
我只是遗漏了一些明显的东西,累了,刚接触 C++ 也无济于事。
最佳答案
由于您在 vector 外部迭代并因此发生崩溃,只需更改 index<=length
至 index != length
它应该可以工作
关于c++ - 列出 vector 元素时程序崩溃 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607242/