vector::erase()返回下一个iter:
//清除[first, last)中的所有元素 iterator erase(iterator first, iterator last) { iterator i = copy(last, finish, first); destroy(i, finish); finish = finish - (last - first); return first; } //清除某个位置上的元素 iterator erase(iterator position) { if(position + 1 != end()) copy(position + 1, finish, position); --finish; destroy(finish); return position; }
用法:
for(vector<int>::iterator it = vecInt.begin(); it != vecInt.end();) { if(*it == 0) { it = vecInt.erase(it); } else { it++; } }
map::erase()没有返回下一个iter:
(1) void erase (iterator position) { t.erase(position); } (2) size_type erase (const key_type& x) { return t.erase(x); } (3) void erase (iterator first, iterator last) { t.erase(first, last); }
用法:
for(map<int,int>::iterator it = mapInt.begin(); it != mapInt.end();) { if(it->second == 0) { mapInt.erase(it++); } else { it++; } }