1.之前在做相关的操作的时候,涉及到清除list相关的元素,因此会用到erase和remove,那么二者有什么区别呢? 从官方文档中,我们可以获取以下信息 erase : 说明:Removes from the list container either a single element (position) or a range of elements ([first,last)).This effectively reduces the container size by the numbe
错误写法: map<int, int> m; for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) { m.erase(it); } 这样会导致程序行为不可知.因为map是关联容器,对于关联容器来说,如果某一个元素已经被删除,那么其对应的迭代器就失效了,不应该再被使用:否则会导致程序无定义的行为. 正确写法1(STL推荐写法): map<int, int> m; for (map<int