C++——list中erase和remove的区别
1.之前在做相关的操作的时候,涉及到清除list相关的元素,因此会用到erase和remove,那么二者有什么区别呢?
从官方文档中,我们可以获取以下信息
erase :
说明:Removes from the list container either a single element (position) or a range of elements ([first,last)).This effectively reduces the container size by the number of elements removed, which are destroyed.以iterator为单元,对元素进行清除。
返回值:An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.
remove:
说明:Remove elements with specific value。Removes from the container all the elements that compare equal to val. This calls the destructor of these objects and reduces the container size by the number of elements removed.
返回值:空
erase是以迭代器为基本单位,清除元素,改变size的值;remove是以value相等为标准,也改变size的值。
2.在清空list中,我们该用什么操作
//调用析构函数,清掉了list的内存
for (list<CUnit *>::iterator it = listStr.begin(); it != listStr.end(); )
{
delete *it;
listStr.erase(it++);
//listStr.remove(*it++);
}
listStr.clear();
此处不要用remove,什么样的函数,就干什么样子的事情,别瞎用c++的函数。
3.另外一个问题,为什么erase(it++)?
从1中,我们可以看到erase的返回值是iterator。An iterator pointing to the element that followed the last element erased by the function call(指向erase元素的后一个元素的迭代器)。
于是我们有了以下清除方法:
#include"Allinclude.h" int main()
{ cout<<endl<<"map:"<<endl;
map<char, int> mymap;
// insert some values:
mymap['a'] = ;
mymap['b'] = ;
mymap['c'] = ;
mymap['d'] = ;
mymap['e'] = ;
mymap['f'] = ;
for (map<char, int>::iterator it = mymap.begin(); it != mymap.end();)
{
#if 1
if (it->second == )
{
//For the key - based version(2), the function returns the number of elements erased.
mymap.erase(it++);
}
else
{
it++;
}
#endif
//mymap.erase(it++);
}
for (map<char, int>::iterator it = mymap.begin(); it != mymap.end();it++)
{
cout << it->second << " , ";
} cout<<endl<<"vector:"<<endl; vector<int> myvector; // set some values (from 1 to 10)
for (int i = ; i <= ; i++)
myvector.push_back(i);
for (vector<int>::iterator it = myvector.begin(); it != myvector.end();)
{
#if 0
if (*it == )
{
myvector.erase(it++);
//等同于it=myvector.erase(it);因为返回值是下一个值的迭代器
}
else
{
it++;
}
#endif
myvector.erase(it);
} // set some values (from 1 to 10)
for (int i = ; i < myvector.size(); i++)
{
cout << myvector[i] << " , ";
} cout<<endl<<"list:"<<endl;
list<int> mylist; // set some values:
for (int i = ; i < ; ++i)
mylist.push_back(i * );
for (list<int>::iterator it = mylist.begin(); it != mylist.end();)
{
#if 1
if (*it == )
{
mylist.erase(it++);
//等同于it=myvector.erase(it);因为返回值是下一个值的迭代器
//it = mylist.erase(it);
}
else
{
it++;
}
#endif
//mylist.erase(it++);
} // set some values (from 1 to 10)
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
{
cout << *it << " , ";
} system("pause");
return ;
}
其实我建议还是用
it = mylist.erase(it)
代码更加清晰一点。
其实最好还是用erase(begin(),end());除非要自己清除一些成员内存。
参考文献:
https://blog.csdn.net/liuzhi67/article/details/50950843
C++——list中erase和remove的区别的更多相关文章
- jquery中empty()和remove()的区别
empty 顾名思义,清空方法,但是与删除又有点不一样,因为它只移除了 指定元素中的所有子节点. remove与empty一样,都是移除元素的方法,但是remove会将元素自身移除,同时也会移除元素内 ...
- 在 Queue 中 poll()和 remove()有什么区别?(未完成)
在 Queue 中 poll()和 remove()有什么区别?(未完成)
- 如何实现数组与List的相互转换?在 Queue 中 poll()和 remove()有什么区别?哪些集合类是线程安全的?
如何实现数组与List的相互转换? List转数组:toArray(arraylist.size()方法 数组转List:Arrays的asList(a)方法 /** * 〈一句话功能简述〉; * 〈 ...
- C++中vector的remove用法
我将从remove的复习开始这个条款,因为remove是STL中最糊涂的算法.误解remove很容易,驱散所有关于remove行为的疑虑——为什么它这么做,它是怎么做的——是很重要的. 这是rem ...
- java中fail-fast 和 fail-safe的区别
java中fail-fast 和 fail-safe的区别 原文地址:http://javahungry.blogspot.com/2014/04/fail-fast-iterator-vs-fa ...
- .Net 中DataTable和 DataRow的 区别与联系
1.简要说明二者关系 DataRow 和 DataColumn 对象是 DataTable 的主要组件.使用 DataRow 对象及其属性和方法检索.评估.插入.删除和更新 DataTable 中的值 ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
- 腾讯一面!说说ArrayList的遍历foreach与iterator时remove的区别,我一脸懵逼
本文基于JDK-8u261源码分析 1 简介 ArrayList作为最基础的集合类,其底层是使用一个动态数组来实现的,这里"动态"的意思是可以动态扩容(虽然ArrayList可 ...
- 【转】为什么我们都理解错了HTTP中GET与POST的区别
GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...
随机推荐
- Python之OS内置模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curd ...
- Vue语法学习第三课——计算属性
模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.对于任何复杂逻辑,都应当使用计算属性. <div id="example&qu ...
- dubbo的常用配置(基于注解)
之前记录了基于springboot的dubbo入门案例,今天在此基础上记录dubbo官网介绍的常用属性配置,dubbo读取我们配置的属性时是有优先级的,优先级如下图: 如图所示,优先级的属性依次为虚拟 ...
- 使用restTemplate来访问https
1.maven: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId& ...
- HTML+CSS水平垂直居中
啦啦啦,好了,今天来分享自己的第一个知识点,难得自己还能想起来过来博客园,写写博客的. 好了,言归正传,今天分享关于html和css的一个简单的知识点,对于大部分从事前端开发的人员来说可能都是很简单的 ...
- svg 动画 透明度 放大缩小 x轴Y轴
参考链接:https://www.cnblogs.com/Chrimisia/p/6670303.html vue 中封装svg:http://www.cnblogs.com/Jiangchuanwe ...
- SVN配置(服务器端及客户端)
版本控制工具,在前几年一直用的SVN,从去年后半年开始本人开始使用GIT.为了避免后期再次使用SVN过程中出现生疏,在此将SVN服务端及客户端配置方式罗列出来. 本人使用的SVN版本为 服务端: Vi ...
- 王者荣耀交流协会final发布-第3次scrum立会
1.例会照片 成员高远博,冉华,王磊,王玉玲,任思佳,袁玥出席.拍照的是王磊同学,王超同学因参加比赛不在学校,不能出席. master:任思佳 2.时间跨度 2017年12月3日 18:00 — 18 ...
- 导入PrefixHeader.pch 报错UNknow The type "NSString",等基础类
进入到项目,在Buid Settings收索Compile Source 把Compile Source As 改成Objective-C问题即可解决.
- CSS背景样式和列表样式
background-color 设置元素的背景颜色 background-image 把图像设置为背景 background-position 设置背景图像的起始位置 background-atta ...