正向迭代器:
int a[] = {1,2,3,4,5,6};
vector<int> vec(a,a+sizeof(a)/sizeof(int));
// 删除正向迭代器
for (auto iter = vec.begin(); iter != vec.end();) {
if(*iter == 3) {
iter = vec.erase(iter);
} else {
cout << *iter << endl;
iter++;
}
}
反向迭代器:
// 删除逆向迭代器
for (auto iter = vec.rbegin(); iter != vec.rend();) {
if(*iter == 5) {
vec.erase((++iter).base());
} else {
iter++;
}
}