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 }
STL——遍历 删除 set 元素的更多相关文章
- java集合遍历删除指定元素异常分析总结
在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又 ...
- set循环遍历删除特定元素
使用Iterator迭代器 public class Demo { public static void main(String[] args) { Set<Object> obj = n ...
- [STL] 遍历删除两个vector中交集
#include <vector> #include <string> #include <algorithm> using namespace std; int ...
- vector list map 遍历删除指定元素
#include <stdio.h> #include <stdint.h> #include <vector> #include <list> #in ...
- STL中用erase()方法遍历删除元素 .xml
pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...
- STL中用erase()方法遍历删除元素
STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...
- stl 自定义排序与删除重复元素
转: STL—vector删除重复元素 STL提供了很多实用的算法,这里主要讲解sort和unique算法. 删除重复元素,首先将vector排序. sort( vecSrc.begin(), vec ...
- python循环删除列表元素常见错误与正确方法
python循环删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 常见错误 常见错误一:使用固定长度循环删除列表元素 # 使用固定长度循环pop方法删除列表元素 num_list_ ...
- 如何python循环中删除字典元素
//下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...
随机推荐
- 取得 APP 自己的版本号 (狠跨 4 个平台)
XE7 源码下载:[原创]取得APP自己的版本号(狠跨4个平台)XE7.zip XE6 源码下载:[原创]取得APP自己的版本号(狠跨4个平台)XE6.zip XE5 源码下载:[原创]取得APP自己 ...
- Erlang垃圾回收机制的二三事
声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...
- 「Ionic」創建新項目
1.創建新項目 创建一个名为myApp的还有tabs的项目(ionic start <project-name> <optional-template>) 可选模板为sidem ...
- mysql 常用
create database jobs;grant all on jobs.* to root@'%' identified by '111111';flush privileges;
- css通用小笔记03——浏览器窗口变小 div错位的问题
我最近写网页的时候,经常碰到一个普遍的问题,经过我的查阅和尝试,终于解决了这一问题,这里有两种方法提供给大家,如果博友还有更好的方法,欢迎补充. 一.使用min-width属性: 我们先看看下面这段代 ...
- 如何用JavaScript探测CSS动画是否已经完成
不啰嗦上代码: WN:(function(){ var el = $('<fakeelement>'), transition="transition", transi ...
- button与input[type=”button”]的区别
button与input[type="button"]的区别 特别感谢 @守护晴天 ,指出了博客中不细致不严谨的地方,也让我学到了更多,更多是觉得抱歉,由于自己的不细致可能误导了一 ...
- AE,按照属性值关系选择要素
if(axMapControl2.LayerCount<=0) { MessageBox.Show("请加载图层后使用该功能","系统提示",Messag ...
- MS15-020漏洞测试
名称:Microsoft DLL植入远程代码执行漏洞(CVE-2015-0096)(MS15-020) 了解:Microsoft Windows没有正确处理DLL文件的加载,存在远程代码执行漏洞,通过 ...
- Atitit.在线充值功能的设计
Atitit.在线充值功能的设计 1. 流程1 2. Js sdk api 增加订单1 3. Java api 返回servlet处理1 3.1. 返回网址的本地host测试2 1. 流程 本地增 ...