wro*_*ame 2 c++ crash insert vector
这是我所拥有的函数的第一部分导致我的程序崩溃:
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";
...
Run Code Online (Sandbox Code Playgroud)
它在sorted部件处崩溃,因为我可以在屏幕上看到"here"但不是"它失败了".出现以下错误消息:
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.
Run Code Online (Sandbox Code Playgroud)
我不确定现在是什么导致了这个问题,因为我在其他地方有一个类似的代码行student.insert(student.begin() + position(temp, student), temp);没有崩溃(position返回一个int并且temp是struct Student的另一个声明).我该怎么做才能解决问题,第一个插入与第二个插入有什么不同?
它应该是:
sorted.insert(sorted.begin(), student[0]);
Run Code Online (Sandbox Code Playgroud)
您从错误的实例传递迭代器.