这是导致我的程序崩溃的函数的第一部分:
vector<Student> sortGPA(vector<Student> student) {
vector<Student> sorted;
Student test = student[0];
cout << "here\n";
sorted.insert(student.begin(), student[0]);
cout << "it failed.\n";
...
它在 sorted
部分崩溃,因为我可以在屏幕上看到“here”,但看不到“it failed”。出现以下错误消息:
Debug Assertion Failed!
(a long path here...)
Expression: vector emplace iterator outside range
For more information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
我现在不确定是什么导致了这个问题,因为我在别处有一行类似的代码 student.insert(student.begin() + position(temp, student), temp);
不会崩溃(其中 position
返回一个 int 而 temp
是 Student 结构的另一个声明)。我该怎么做才能解决这个问题,第一个插入与第二个插入有何不同?
最佳答案
应该是:
sorted.insert(sorted.begin(), student[0]);
您从错误的实例传递了迭代器。
关于c++ - vector insert() 导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2748950/