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

本文版权归作者所有。

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

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

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

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

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

本文链接: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 元素的更多相关文章

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

    在使用集合的过程中,我们经常会有遍历集合元素,删除指定的元素的需求,而对于这种需求我们往往使用会犯些小错误,导致程序抛异常或者与预期结果不对,本人很早之前就遇到过这个坑,当时没注意总结,结果前段时间又 ...

  2. set循环遍历删除特定元素

    使用Iterator迭代器 public class Demo { public static void main(String[] args) { Set<Object> obj = n ...

  3. [STL] 遍历删除两个vector中交集

    #include <vector> #include <string> #include <algorithm> using namespace std; int ...

  4. vector list map 遍历删除指定元素

    #include <stdio.h> #include <stdint.h> #include <vector> #include <list> #in ...

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

    pre{ line-height:1; color:#f0caa6; background-color:#2d161d; font-size:16px;}.sysFunc{color:#e54ae9; ...

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

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

  7. stl 自定义排序与删除重复元素

    转: STL—vector删除重复元素 STL提供了很多实用的算法,这里主要讲解sort和unique算法. 删除重复元素,首先将vector排序. sort( vecSrc.begin(), vec ...

  8. python循环删除列表元素常见错误与正确方法

    python循环删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 常见错误 常见错误一:使用固定长度循环删除列表元素 # 使用固定长度循环pop方法删除列表元素 num_list_ ...

  9. 如何python循环中删除字典元素

    //下面这行就是在循环中遍历删除字典元素的方法! for i in list(dictheme2.keys()): if dictheme2[i]<self.countFortheme: dic ...

随机推荐

  1. Eclipse常用的快捷键

    Eclipse常用快捷键 1,       Ctrl+S,保存文档. 2,       Tab,多行同时右移:选中多行 3,       shlft+tab,多行同时左移,选中多行 4,       ...

  2. 自学H5第二天

    笔记: 1.css之外联样式 2.css之行间样式: 3.css之内联样式 二.边框的知识: 1.边框的复合样式: 2.边框的单一样式: /*单一样式*/ border-width: 1px 2px ...

  3. 快速熟悉Velocity

    果然公司用的东西跟平时学的东西不太一样,我们公司前台页面并不是我们熟悉的.html或者.jsp文件,而是很多人不知道的 .vm文件,其实只要我们理解了jsp文件,vm文件也就是一些基本语法不同而已. ...

  4. ahjesus配置vsftpd虚拟用户在Ubuntu

    网上搜索了很多资料,过时,不全,货不对版 已下步骤亲测有效,不包含匿名用户登录 1.新建/home/loguser.txt 并填充内容,格式如下 用户名密码用户名密码用户名密码 2.生成db文件用于用 ...

  5. 【转】MyEclipse 2015优化技巧

    MyEclipse 2015优化技巧 MyEclipse 2015优化速度方案仍然主要有这么几个方面:去除无需加载的模块.取消冗余的配置.去除不必要的检查.关闭更新. 第一步: 去除不需要加载的模块 ...

  6. Java识别操作系统

    /** * Created by xfyou on 2016/11/3. */ public class SysDep { final static String UNIX_NULL_DEV = &q ...

  7. net user命令集合详解

    net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user: ...

  8. svg-高斯模糊+swiper伦播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. jquery的insertBefore(),insertAfter(),after(),before()

    insertBefore():a.insertBefore(b) a在前,b在后, a:是一个选择器,b:也是一个选择器 <!DOCTYPE html> <html> < ...

  10. JavaScriptOO.com – 快速找到你需要的 JS 框架

    JavaScriptOO.com 集合了目前 Web 开发中最常用的422(截至目前)款 JavaScript 框架,你可以根据功能类别(Ajax,动画,图表,游戏等)进行过滤和排序,快速找到你需要的 ...