set.clear();             //清除所有元素

set.erase(pos);     //删除pos迭代器所指的元素,返回下一个元素的迭代器。

set.erase(beg,end);    //删除区间[beg,end)的所有元素,返回下一个元素的迭代器。

set.erase(elem);     //删除容器中值为elem的元素。

代码例子:

 1 #include <iostream>
2 #include <set>
3
4 using namespace std;
5
6 int main()
7 {
8 set<int> setInt;
9
10 cout << "第一次遍历setInt,没有任何元素:";
11 for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
12 {
13 cout << *it << " ";
14 }
15
16 //在容器中插入元素
17 cout << endl << "插入20个元素" << endl << endl;
18 for (int i = 0; i < 20; i++)
19 {
20 setInt.insert(i);
21 }
22 cout << "插入20个元素后的第二次遍历setInt" << endl;
23 for (set<int>::iterator it = setInt.begin();it!=setInt.end(); it++)
24 {
25 cout << *it << " ";
26 }
27 cout << endl;
28
29 //删除迭代器所指的元素,返回下一个元素的迭代器。
30 cout << "删除迭代器所指的元素 5 " << endl;
31 for (set<int>::iterator it = setInt.begin(); it != setInt.end();)
32 {
33 if (*it == 5)
34 {
35 it = setInt.erase(it); //由于会返回下一个元素的迭代器,相当于进行了it++的操作
36 }
37 else
38 {
39 it++;
40 }
41 }
42 cout << endl << "删除迭代器所指的元素 5 后遍历 setInt:" << endl;
43 for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
44 {
45 cout << *it << " ";
46 }
47 cout << endl;
48
49 //删除区间(beg,end)的所有元素,返回下一个元素的迭代器。
50 cout << endl << "删除元素 15 之后的所有元素";
51 for (set<int>::iterator it = setInt.begin(); it != setInt.end();)
52 {
53 if (*it == 15)
54 {
55 //如果找到15,删除15之后所有的元素,由于会返回下一个元素的迭代器,相当于进行了it++的操作
56 it = setInt.erase(it, setInt.end());
57 }
58 else
59 {
60 it++;
61 }
62 }
63 cout << endl << "删除元素 15 之后的所有元素后遍历 setInt:";
64 for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
65 {
66 cout << *it << " ";
67 }
68 cout << endl;
69
70 // 删除容器中值为elem的元素
71 cout << endl << "删除元素 10";
72 setInt.erase(10);
73 cout << endl << "删除元素 10 之后遍历 setInt:";
74 for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
75 {
76 cout << *it << " ";
77 }
78 cout << endl;
79
80 //清除所有元素
81 cout << endl << "删除所有元素";
82 setInt.clear();
83 cout << endl << "删除所有元素之后遍历 setInt:";
84 for (set<int>::iterator it = setInt.begin(); it != setInt.end(); it++)
85 {
86 cout << *it << " ";
87 }
88 cout << endl;
89
90 return 0;
91 }

打印结果:

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

STL——容器(Set & multiset)的删除 erase的更多相关文章

  1. STL Set和multiset 容器

    STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...

  2. STL容器及算法题:删除奇数的QQ号

    最近思考到这样一个题目:在STL的set和vector容器里存储了1亿个QQ号,编写函数删除奇数QQ号. 1. STL容器简介 首先了解一下 set 和 vector 以及其他类似的 STL 容器: ...

  3. STL容器内数据删除

    STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...

  4. STL容器删除元素的陷阱

    今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector< ...

  5. STL容器的遍历删除

    STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的 ...

  6. STL容器 erase的使用陷井

    http://www.cppblog.com/beautykingdom/archive/2008/07/09/55760.aspx?opt=admin 在STL(标准模板库)中经常会碰到要删除容器中 ...

  7. STL容器迭代过程中删除元素技巧(转)

    1.连续内存序列容器(vector,string,deque) 序列容器的erase方法返回值是指向紧接在被删除元素之后的元素的有效迭代器,可以根据这个返回值来安全删除元素. vector<in ...

  8. 怎么删除STL容器的元素

    在STL容器有顺序容器和关联容器两种. 顺序容器删除元素的方法有两种: 1.c.erase(p) 从c中删除迭代器p指定的元素.p必须指向c中一个真实元素,不能等于c.end().返回一个指向p之后元 ...

  9. 删除STL容器中的元素

    有关stl容器删除元素的问题,错误的代码如下: std::vector<struct> mFriendList; ... std::vector<struct>::iterat ...

  10. STL——容器(Set & multiset)的默认构造 & 带参构造 & 对象的拷贝构造与赋值

    1. 默认构造 set<int> setInt;              //一个存放int的set容器. set<float> setFloat;          //一 ...

随机推荐

  1. Debian 64位内核升级步骤

    安装相关依赖包 apt-get install bzip2 libncurses5-dev kernel-package zlib1g-dev gcc make kernel-package wget ...

  2. Python_faker (伪装者)创建假数据

    faker (伪装者)创建假数据 工作中,有时候我们需要伪造一些假数据,如何使用 Python 伪造这些看起来一点也不假的假数据呢? Python 有一个包叫 Faker,使用它可以轻易地伪造姓名.地 ...

  3. 不要再说不会Spring了!Spring第一天,学会进大厂!

    工作及面试的过程中,作为Java开发,Spring环绕在我们的身边,很多人都是一知半解,本次将用14天时间,针对容器中注解.组件.源码进行解读,AOP概念进行全方面360°无死角介绍,SpringMV ...

  4. swlib

    Swoole人性化组件库 ~ Swoole Humanization Component Library

  5. 常见web漏洞修复方法

    方法如下: 漏洞修复.(输入过滤,输出转义) 1.在连接数据库时,在接收参数后进行转义,$id = mysql_real_escape_string($id); 2.在网页源码中在接收参数后可用htm ...

  6. css3系列之box-sizing

    box-sizing box-sizing: 俗称ie6 的混杂模式的盒子模型.  首先来了解一下 ie6 的混杂模式,和我们常用的 盒子模型有什么不一样 正常模式下: 我们设置的 width  和  ...

  7. Python Api接口自动化测试框架 代码写用例

    公司新来两个妹子一直吐槽这个接口测试用例用excel维护起来十分费脑费事,而且比较low(内心十分赞同但是不能推翻自己),妹子说excel本来就很麻烦的工具,于是偷偷的进行了二次改版. 变更内容如下: ...

  8. iOS7使用iOS8上的方法报错处理

    问题描述 我们经常会遇到在低版本上使用高版本方法导致的bug,例如: WebKit discarded an uncaught exception in the webView:decidePolic ...

  9. Python GUI之Tkiner实战

    前言 Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序. 由于 Tkinter 是内置到 python 的安装包中.只要安装好 ...

  10. Kafka分布式查询引擎

    1.概述 Kafka是一个分布式消息中间件系统,里面存储着实际场景中的数据.Kafka原生是不支持点查询的,如果我们想对存储在Topic中的数据进行查询,可能需要对Topic中的数据进行消费落地,然后 ...