c++ - 列出 vector 元素时程序崩溃 (C++)

标签 c++ crash

我试图在 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<=lengthindex != length它应该可以工作

关于c++ - 列出 vector 元素时程序崩溃 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16607242/

相关文章:

c++ - 使用 MAKEWORD 函数会产生错误 E0109- 明显调用的括号前的表达式必须具有(指向-)函数类型

需要 C++ 运行时?

c++ 2d数组传递给函数

java - 当我单击按钮更改 Activity 时应用程序崩溃

iPhone 应用程序在 iOS 4.0 上崩溃

delphi - Delphi XE 2在启动时崩溃

c++ - 引用传递以错误结束

c++ - 超过 5 个参数的绳索表达模板无法正确内联

android - React Native 应用程序崩溃 : Unable to load script from assets 'index.android.bundle'

.net - VS 2010 .NET 应用程序发布在 XP 或 Server 2003 上编译崩溃