我使用 reverse_iterator 查找我的向量并使用 pop_back 擦除元素。但它在调试模式下会导致一些错误。我的代码是这样的:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Student{
string name;
int score;
};
int main()
{
vector<Student> testVec;
testVec.push_back(Student{ "Lilei",50 });
testVec.push_back(Student{ "YangFeifei",80 });
testVec.push_back(Student{ "WuMing",80 });
for (auto it = testVec.rbegin(); it != testVec.rend(); ++it){
if (it == testVec.rbegin()) continue;
while (it != testVec.rbegin()) {
std::cout << &(*testVec.rbegin()) << ", ";
std::cout << &(*it) << std::endl;
testVec.pop_back();
std::cout << &(*testVec.rbegin()) << ", ";
std::cout << &(*it) << std::endl; // where error occur!
}
}
std::cout << "Hello World!\n";
}