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 ...
随机推荐
- 用于PHP的Gearman Worker管理工具GearmanManager
项目地址:https://github.com/brianlmoon/GearmanManager PHP环境要求 PHP 5.5.9 POSIX extension Process Control ...
- 点我吧工作总结(技术篇) Cobar原理和环境搭建
我思故我在,提问启迪思考! 1.什么是Cobar? Cobar是关系型数据的分布式处理系统,它可以在分布式的环境下看上去像传统数据库一样为您提供海量数据服务.cobar已经在阿里巴巴B2B公司稳定运行 ...
- java 用 jxl poi 进行excel 解析 *** 最爱那水货
/** * 解析excel文件 ,并把数据放入数组中 格式 xlsx xls * @param path 从ftp上下载到本地的文件的路径 * @return 数据数组集合 */ public Lis ...
- linux常用命令之文件管理
LS ls:list directory contents 默认情况 默认情况下显示的是mtime 选项 -a 列出全部文件及目录包括隐藏的 -l 列出详细信息,包括文件类型.权限.节点.owner. ...
- Xamarin安装和跳坑指南
安装Checklist 注意:本文只描述安装过程,由于组件的版本更新很快,为保证文章时效性,不提供下载链接,也尽可能不指明具体版本. 安装Visual Studio 2015进行默认安装,除非已经FQ ...
- Tab切换类型
Tab切换类型 点击Tab 滑过Tab 延迟Tab CSS样式 ; ; list-style:none; font-size:12px;} .notice{width:298px; height:98 ...
- Navicat for Oracle实现连接Oracle
不知道为什么,从一开始,我就不喜欢Oracle,名字好听,功能强大,但总感觉"高不可攀";或许是因为我觉得其他的数据库就可以解决数据问题,不太了解Oracle的优势:而且它长得也不 ...
- Shapely中的几何图形操作
Geometric Objects object.area Returns the area (float) of the object. object.bounds Returns a (minx, ...
- arcgis批量处理mxd定义服务中的路径
>>> from arcpy import env... env.workspace=r"c:\165mxd"... out = r"c:\166mx ...
- Merry Christmas & Happy New Year!!
圣诞快乐,新年快乐!