STL——遍历 删除 set 元素

==================================声明==================================

本文版权归作者所有。

本文原创,转载必须在正文中显要地注明作者和出处,并保证文章(包括本声明)的完整性。

未经作者授权请勿修改(包括本声明),保留法律追究的权利。

未经作者授权请勿用于学术性引用。

未经作者授权请勿用于商业出版、商业印刷、商业引用以及其他商业用途。

本文不定期修正完善,为保证内容正确,建议移步原文处阅读。

本文链接:http://www.cnblogs.com/wlsandwho/p/4468023.html

=======================================================================

STL是个好东西,在客户端上用一用没什么问题。

在使用multimap时,伴随一个set来统计multimap中key的种类。真是省心省力。

然而,时间换空间、空间换时间。伴随set会带来开销。

世间安得双全法?那必定是晦涩难懂的,不能在普罗大众间流传。

=======================================================================

以前一直没怎么注意遍历删除set。当我随手写了个小代码后,我想知道人类是怎么做的。

于是搜一搜。

不知为何,网上真是转载文章一大抄,这也罢了,可为何STL遍历删除的例子都要列上错误做法?

=======================================================================

set的erase并不返回iterator,所以在遍历删除的时候,要使用

void erase (iterator position);

函数。

一个不错的小网站http://www.cplusplus.com/reference/

=======================================================================

贴上我的纯手工小代码。

 1 #include <iostream>
 2 #include <set>
 3
 4 using namespace std;
 5
 6 int main()
 7 {
 8     set<int> setIntTest;
 9     set<int>::iterator itsetIntTest;
10
11     setIntTest.insert(1);
12     setIntTest.insert(2);
13     setIntTest.insert(3);
14     setIntTest.insert(4);
15     setIntTest.insert(5);
16     setIntTest.insert(6);
17     setIntTest.insert(7);
18
19     wcout<<L"Before:"<<endl;
20     for (itsetIntTest=setIntTest.begin();itsetIntTest!=setIntTest.end();itsetIntTest++)
21     {
22         wcout<<*itsetIntTest<<endl;
23     }
24
25     //////////////////////////////////////////////////////////////////////////
26     wcout<<L"Remove those can not be divided by 3:"<<endl;
27     for (itsetIntTest=setIntTest.begin();itsetIntTest!=setIntTest.end();)
28     {
29         if ((*itsetIntTest)%3!=0)
30         {
31             wcout<<L"Remove\t"<<*itsetIntTest<<endl;
32
33             setIntTest.erase(itsetIntTest++);//必然是擦除后再移动迭代器,所以后++
34         }
35         else
36         {
37             itsetIntTest++;//这个就无所谓前后了,没有涉及增删操作。
38         }
39     }
40
41     //////////////////////////////////////////////////////////////////////////
42     wcout<<L"After:"<<endl;
43     for (itsetIntTest=setIntTest.begin();itsetIntTest!=setIntTest.end();itsetIntTest++)
44     {
45         wcout<<*itsetIntTest<<endl;
46     }
47
48     return 0;
49 }
时间: 2024-08-06 14:51:22

STL——遍历 删除 set 元素的相关文章

java集合遍历删除指定元素异常分析总结

在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又遇到了这个问题,因此,总结下遍历集合的同时如何删除集合中指定的元素: 1.错误场景复原 public class ListRemoveTest { public static void main(String[] args) { List<User> users = new ArrayList&l

STL中用erase()方法遍历删除元素?.xml

pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9;font-style:italic;font-weight:bold;} .selfFuc{color:#f1f9be;} .bool{color:#69305e;} .condition{color:#628698;font-weight:bold;} .key{color:#e336b6;} .

STL容器的遍历删除

STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的表项(当然这个函数是应该运行在另起一个线程上的),但是在按照下面的方法对hash_map(用迭代器)遍历删除时,当找到第一个满足删除条件的元素并将其删除后,程序将提示非法: for(list<int>::iterator iter = m_map.begin(); iter != m_map.en

遍历list集合删除指定元素方法

今天的在项目中犯的一个错误记录一下: 刚开始粗心写成for喜欢遍历 这样会出现下表越界的问题 应该是iterate遍历移除集合中的元素 以下是转载: 一种错误的方式: for(int i = 0 , len= list.size();i<len;++i){     if(list.get(i)==XXX){          list.remove(i);     }   } 上面这种方式会抛出如下异常: Exception in thread "main" java.lang.

Python遍历删除元素

需求: 有一个列表:ls = [1,2,3,4,5,6],要求删除其中的偶数 如果是下面这种方法,在遍历的过程中删除,会有问题 for i in range(len(ls)): if ls[i]%2 == 0: del ls[i] 异常:IndexError: list index out of range 因为删除一个元素后,列表的长度改变了,所以会报下标越界. 可以采取一下三种方法: 1. ls = [x for x in ls if x%2!=0] #列表解析 2. ls = filter

如何正确遍历删除List中的元素

遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合条件的一个元素 3.通过普通的for删除删除符合条件的多个元素 4.通过Iterator进行遍历删除符合条件的多个元素 /** * 使用增强的for循环 * 在循环过程中从List中删除元素以后,继续循环List时会报ConcurrentModificationException */ public

C#遍历List并删除某个元素的方法

C#遍历List并删除某个元素的方法,你的第一反应使用什么方法实现呢?foreach? for? 如果是foreach,那么恭喜你,你答错了.如果你想到的是用for,那么你只是离成功进了一步. 正确的做法是用for倒序遍历,根据条件删除.下面我们用代码来演示foreach,for删除list数据的情况: class Program { public class Students { public string Name { get; set; } public int Age { get; se

stl map高效遍历删除的方法 [转]

for(:iter!=mapStudent.end():) { if((iter->second)>=aa) { //满足删除条件,删除当前结点,并指向下面一个结点 mapStudent.erase(iter++): } else { //条件不满足,指向下面一个结点 iter++: } } 这种删除方式也是STL源码一书中推荐的方式,分析 mapStudent.erase(iter++)语句,map中在删除iter的时候,先将iter做缓存,然后执行iter++使之指向下一个结点,再进入er

STL容器删除元素的陷阱

今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector<struct> mFriendList;...std::vector<struct>::iterator iter = mFriendList.begin();for ( ; iter != mFriendList.end(); ++iter){    if (...)        mFr