stl iterator的坑
- 先不说在for循环里erase会出现的bug。
- list的iterator几乎等于指向元素的指针。
int main() {
vector<int> a{1, 2, 3, 4};
list<int> b{1, 2, 3, 4};
vector<int>::iterator ai = a.begin();
list<int>::iterator bi = b.begin();
a.erase(a.begin());
cout << *ai << endl;
cout << *(a.begin()) << endl;
b.erase(b.begin());
cout << *bi << endl;
cout << *(b.begin()) << endl;
return 0;
}
输出是:
2
2
1
2