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传递参数. 你可能自己 ...
随机推荐
- springboot 热部署
1 pom文件添加 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- 连接MySQL常用工具
database.properties 如下:url中coursesystem为将要连接的数据库名:username为该数据库设置权限时的用户名:如果设置了密码,再添一项password=你的密码 d ...
- 酷学习笔记——ASP.NET Core 简介
ASP.NET Core 简介 其实就是说酷好,不好好学,不学好,没饭吃. 新词汇:IoT,Internet of Things,网联网,微软物联网英文网站.微软物联网中文网站
- jQuery常用 遍历函数
jQuery 遍历函数包括了用于筛选.查找和串联元素的方法.本文主要介绍日常工作中常用的JQ遍历,帮助一下初学者快速的接触遍历函数,提高自己的代码编写速度,写出更简洁更实用的代码,祝前端的同学们,在前 ...
- node.js 之 N-blog
N-blog 使用 Express + MongoDB 搭建多人博客 原文地址: https://github.com/nswbmw/N-blog 建议初学者,研究下整个项目. 这里节选了一些内容为 ...
- ios 传递JSON串过去 前面多了个等号
先说下我的问题 后台让我这边把请求的参数弄成一个实体转化成 json 串放body里传给他,当然header也有设置,提前设置好了, 但是后来了解 所谓的把实体转成json串的本质就是先把实体用run ...
- python 解方程
[怪毛匠子=整理] SymPy 库 安装 sudo pip install sympy x = Symbol('x') 解方程 solve([2 * x - y - 3, 3 * x + y - 7] ...
- 安装pipenv
首先: 安装pipenv pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pipenv 使用国内源安装pipenv 创建文件夹 mkd ...
- 自动删除Android工程中无用的资源
开发时间久了, 几个版本迭代之后, 工程中难免留下很多垃圾资源, 造成apk的包很大, 这里介绍一个工具, 可以自动扫描工程中, 没有使用的资源, 然后自动删除: 包括图片, xml, 文本等. 采用 ...
- excel 用VBA将所有单元格内容全部转换为文本
Sub 将所有列全部转换为文本() t=timer 'Cells(Rows.Count, 1).End(xlUp).Row 获取第一列最后一个非空单元格的行号 s = Cells(, Columns. ...