STL - 移除(remove)和释放(erase)集合元素
remove(移除):
这个操作并不是真正地删除元素,它会移除指定的元素,然后后面的元素依次前移,最后用别的元素来补充。
erase(释放):
这个操作会指定释放区间的头和尾迭代器(iterator)。
如果要一次性删除指定元素:
coll.erase(remove(coll.begin(), coll.end(), [removed element]), coll.end());
代码如下:
list<int> coll1;
for (int i = ; i <= ; ++i)
{
coll1.push_front(i);
coll1.push_back(i);
}
cout << "** collection 1: **" << endl;
ContainerUtil<list<int>>::printElements(coll1);
// remove all elements with value 3
list<int>::iterator end = remove(coll1.begin(), coll1.end(), );
cout << "** collection 1(after remove elements 3): **" << endl;
ContainerUtil<list<int>>::printElements(coll1);
// print number of removed elements
cout << "number of removed elements : " << distance(end, coll1.end()) << endl;
// release 'removed' elements
coll1.erase(end, coll1.end());
cout << "** collection 1(after releasing removed elements): **" << endl;
ContainerUtil<list<int>>::printElements(coll1);
// remove & release elements with value 4 all at once
coll1.erase(remove(coll1.begin(), coll1.end(), ), coll1.end());
cout << "** collection 1(after remove & release elements 4): **" << endl;
ContainerUtil<list<int>>::printElements(coll1);
运行结果:
** collection 1: **
6 5 4 3 2 1 1 2 3 4 5 6
** collection 1(after remove elements 3): **
6 5 4 2 1 1 2 4 5 6 5 6
number of removed elements : 2
** collection 1(after releasing removed elements): **
6 5 4 2 1 1 2 4 5 6
** collection 1(after remove & release elements 4): **
6 5 2 1 1 2 5 6
STL - 移除(remove)和释放(erase)集合元素的更多相关文章
- STL笔记(4)关于erase,remove
STL笔记(4)关于erase,remove 你要erase的元素很容易识别.它们是从区间的“新逻辑终点”开始持续到区间真的终点的原来区间的元素.要除去那些元素,你要做的所有事情就是用那两个迭代器调用 ...
- 当JAVA集合移除自身集合元素时发生的诸多问题
一段代码目的是想删除集合中包括"a"字符串的集合项: public class TestForeach { public static void main(String[] arg ...
- Lambda 表达式遍历集合时用remove方法删除list集合中满足条件的元素问题
一:循环遍历list集合的四种方式 简单for循环 iterator循环 增加for循环 Lanbda表达式 二:四种遍历方式的用法示例 //简单for循环 List<SalaryAdjustm ...
- 高效率遍历Map以及在循环过程中移除 remove指定key
//高效率遍历Map以及在循环过程中移除 remove指定key //使用iter循环的时候 可以在循环中移除key,for在循环的过程中移除会报错哦 //本方法效率高 Iterator iter = ...
- STL—内存的配置与释放
上一篇我们介绍了STL对象的构造与析构,这篇介绍STL内存的配置与释放. STL有两级空间配置器,默认是使用第二级.第二级空间配置器会在某些情况下去调用第一级空间配置器.空间配置器都是在allocat ...
- 删除集合元素Collection ,remove()
package seday11;/*** @author xingsir*/public class coordinate { private int x; private int y; /* * 右 ...
- Java集合——遍历集合元素并修改
Java集合——遍历集合元素并修改 摘要:本文主要总结了遍历集合的方式,以及在遍历时修改集合要注意的问题. 遍历Collection 对List和Set的遍历,有四种方式,下面以ArrayList为例 ...
- Java修炼——ArrayList常用的方法以及三种方式遍历集合元素。
List接口ArrayList用法详解 ArrayList常用方法: 1. List.add():添加的方法(可以添加字符串,常量,以及对象) List list=new ArrayList(); l ...
- java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);
java.util.Collection接口 Java的集合主要由两个接口派生出来,一个是Collection一个是Map,本章只记录Collection常用集合 集合只能存储引用类型数据,不能存储基 ...
随机推荐
- SpringBoot学习(七)
静态资源处理 spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot ...
- ubuntu 安装qq 及解决安装完搜狗输入法不显示键盘的方法
安装qq: https://zhuanlan.zhihu.com/p/27549700 解决搜狗输入法不显示的问题: http://blog.csdn.net/crystal_zero/article ...
- thunk 函数
function* f() { console.log(1); for (var i = 0; true; i++) { console.log('come in'); var reset = yie ...
- POJ poj 2155 Matrix
题目链接[http://poj.org/problem?id=2155] /* poj 2155 Matrix 题意:矩阵加减,单点求和 二维线段树,矩阵加减,单点求和. */ using names ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- 51nod1515 明辨是非 并查集 + set
一开始想的时候,好像两个并查集就可以做......然后突然懂了什么.... 相同的并查集没有问题,不同的就不能并查集了,暴力的来个set就行了..... 合并的时候启发式合并即可做到$O(n \log ...
- [HAOI2012]外星人
题目大意: 告诉你一个数n,求满足φ^x(n)=1的x. 思路: 首先我们可以发现满足φ(n)=1的数只有2,也就是说你得到最终的结果,最后一步肯定是φ(2). 同时,可以发现φ(φ(2^k))=φ( ...
- 零配置文件搭建SpringMvc
零配置文件搭建SpringMvc SpringMvc 流程原理 (1)用户发送请求至前端控制器DispatcherServlet:(2) DispatcherServlet收到请求后,调用Handle ...
- 3524: [Poi2014]Couriers -- 主席树
3524: [Poi2014]Couriers Time Limit: 20 Sec Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组 ...
- PAT甲级1076. Forwards on Weibo
PAT甲级1076. Forwards on Weibo 题意: 微博被称为中文版的Twitter.微博上的一位用户可能会有很多关注者,也可能会跟随许多其他用户.因此,社会网络与追随者的关系形成.当用 ...