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常用集合 集合只能存储引用类型数据,不能存储基 ...
随机推荐
- css加载方式link和@import的区别!
本质上,这两种方式都是为了加载CSS文件,但还是存在着细微的差别. 1. 老祖宗的差别.link属于XHTML标签,而@import完全是CSS提供的一种方式. link标签除了可以加载CSS外,还可 ...
- 【BZOJ 4170】 4170: 极光 (CDQ分治)
4170: 极光 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 121 Solved: 64 Description "若是万一琪露诺(俗 ...
- Wannafly挑战赛17 B
题解 大概就是求证这个 \[\sum_i^nC_{n}^i*C_n^i = C_{2n}^n\] 证明: \[(1+x)^{2n} = [C(0,n)+C(1,n)*x+...+C(n,n)*x^n] ...
- [BZOJ4555][TJOI2016&HEOI2016]求和(分治FFT)
4555: [Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 525 Solved: 418[Sub ...
- AFNetworking3.0 Post JSON数据
使用AFNetworking3.0后网络请求由原来的AFHTTPRequestOperationManager变为AFHTTPSessionManager.一般的Post请求变成了这样: NSMuta ...
- 人脸对齐SDM原理----Supervised Descent Method and its Applications to Face Alignment
最近组里研究了SDM算法在人脸对齐中的应用,是CMU的论文<Supervised Descent Method and its Applications to Face Alignment> ...
- 探究react-native 源码的图片缓存
先看js端图片使用的三种方式,依次排序1.2.3 <Image source={{uri:url}} style={{width:200,height:200}}/> 1. 加载远程图片 ...
- Codeforces Round #247 (Div. 2) ABC
Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431 代码均已投放:https://github.com/illuz/Wa ...
- Open Source Universal 48 pin programmer design
http://www.edaboard.com/thread227388.html Hi, i have designed a 48 pin universal programmer but need ...
- Spring在bean配置文件中定义电子邮件模板
在上一篇Spring电子邮件教程,硬编码的所有电子邮件属性和消息的方法体中的内容,这是不实际的,应予以避免.应该考虑在Spring bean 配置文件中定义电子邮件模板. 1.Spring的邮件发件人 ...