end():超出末端迭代器,也就是末端元素的下一个。
删除所有的2--错误代码:
vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(2);
ivec.push_back(3);
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++){
if( *iter == 2){
ivec.erase(iter);
}
}
iterator原型:
iterator erase (iterator position){
if (position + 1 != end()){
copy (position + 1, finish, position);
}
--finish;
destroy (finish);
return position;
}
改正后的代码:
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ){
if ( *iter == 2)
ivec.erase (iter);
else
iter ++ ;
}