c++里面的list,vector的删除方法非常简便
std::vector<int> c { 1,2,3,4,5,6,7 }; int x = 5; c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
然而根据STL std::map中的定义void erase(iterator pos),此erase并不返回下一个元素的迭代器,因此不能采用std::list的方法
The truth is that ‘erase’ in associative containers doesn’t invalidate any iterators except those that point to elements being erased (that’s also true for ’sid::list’). For this reason, you don’t really need ‘map::erase’ to return an iterator. Just do this
for(auto it = map.begin(), ite = map.end(); it != ite;){ if(it->second._id == remove_id) it = map.erase(it); else ++it;}
当然此方法同样也适合于std::list等。
原文:http://mingtangduyao.blog.51cto.com/4876371/1731510