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常用集合 集合只能存储引用类型数据,不能存储基 ...
随机推荐
- FastReport.Net使用:[33]高亮显示
1.首先来看下初始报表,很简单很普通. 2.下面对报表改进,90分以上的成绩以绿色显示,60~70分的以橙色斜体显示. 报表设计中选择数据成绩文本框,然后点击工具栏上的“ab突出显示”按钮打开“高亮显 ...
- [POI2005]A Journey to Mars --- 单调队列
[POI2005]A Journey to Mars 题目描述: Byteazar 决定去火星参加一个空间站旅行. 火星的所有空间站都位于一个圆上. Byteazar 在其中一个登陆然后变开始饶圈旅行 ...
- [POJ1625]Censored!(AC自动机+DP+高精度)
Censored! Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10824 Accepted: 2966 Descri ...
- POJ 2406 Power Strings 简单KMP模板 strcmp
http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...
- php正则给图片提取/替换/添加alt标签的正则代码
有的时候我们需要对富文本编辑器的内容做一些处理,例如图片的alt标签.百度的富文本编辑器添加的图片就是没有的,那么我们要添加就必须使用正则了,下面一起来看看如何实现吧. $preg = "/ ...
- JDK源码(1.7) -- java.util.AbstractList<E>
java.util.AbstractList<E> 源码分析(JDK1.7) ------------------------------------------------------- ...
- c++的atoi和stoi一些区别
c++的atoi和stoi一些区别 对c++标准库中字符串转化为int的两个函数atoi()和stoi()两个有所混乱,特地研究了一下. stoi() 标准库的函数默认模板 int stoi (con ...
- How to implement *All-Digital* analog-to-digital converters in FPGAs and ASICs
When we engineers look at the complexity of system design these days, we are challenged with crammin ...
- source insight完全卸载
由于不知名原因 source insight崩溃了,使用自带的卸载,完成之后重新安装软件注册还是出问题.在网上搜索资料发现就是删除注册表中的内容. 由于列出的删除项目不完全,导致还是出问题. 最后删除 ...
- springboot 选择启动某个配置文件
选择启动某个配置文件 Spring Boot配置文件提供了隔离一部分应用程序配置的方法,并可使其仅在某指定环境可用.任何有@Component和@Configuration注解的Bean都用@prof ...