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的区别的更多相关文章

  1. jquery中empty()和remove()的区别

    empty 顾名思义,清空方法,但是与删除又有点不一样,因为它只移除了 指定元素中的所有子节点. remove与empty一样,都是移除元素的方法,但是remove会将元素自身移除,同时也会移除元素内 ...

  2. 在 Queue 中 poll()和 remove()有什么区别?(未完成)

    在 Queue 中 poll()和 remove()有什么区别?(未完成)

  3. 如何实现数组与List的相互转换?在 Queue 中 poll()和 remove()有什么区别?哪些集合类是线程安全的?

    如何实现数组与List的相互转换? List转数组:toArray(arraylist.size()方法 数组转List:Arrays的asList(a)方法 /** * 〈一句话功能简述〉; * 〈 ...

  4. C++中vector的remove用法

      我将从remove的复习开始这个条款,因为remove是STL中最糊涂的算法.误解remove很容易,驱散所有关于remove行为的疑虑——为什么它这么做,它是怎么做的——是很重要的. 这是rem ...

  5. java中fail-fast 和 fail-safe的区别

    java中fail-fast 和 fail-safe的区别   原文地址:http://javahungry.blogspot.com/2014/04/fail-fast-iterator-vs-fa ...

  6. .Net 中DataTable和 DataRow的 区别与联系

    1.简要说明二者关系 DataRow 和 DataColumn 对象是 DataTable 的主要组件.使用 DataRow 对象及其属性和方法检索.评估.插入.删除和更新 DataTable 中的值 ...

  7. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  8. 腾讯一面!说说ArrayList的遍历foreach与iterator时remove的区别,我一脸懵逼

    本文基于JDK-8u261源码分析 1 简介 ​ ArrayList作为最基础的集合类,其底层是使用一个动态数组来实现的,这里"动态"的意思是可以动态扩容(虽然ArrayList可 ...

  9. 【转】为什么我们都理解错了HTTP中GET与POST的区别

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

随机推荐

  1. .NET界面控件DevExpress发布v18.2.8|附下载

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...

  2. centos 远程授权

    centos 远程授权命令 ssh-copy-id root@192.168.15.70

  3. mybatis分页查询的万能模板

    分页查询项目里太多了,而这种分页查询,在mybatis里面的配置几乎一模一样,今天就整理一个比较好和实用的模板,供以后直接Ctrl+C <select id="queryMember& ...

  4. 【命令】Ubuntu设置和查看环境变量

    转自[Ubuntu]Ubuntu设置和查看环境变量 查看环境变量 env env命令是environment的缩写,用于列出所有的环境变量 export 单独使用export命令也可以像env列出所有 ...

  5. “学习CSS布局” 笔记

    学习网址:http://zh.learnlayout.com/no-layout.html 本文仅为学习笔记,内容非原创. position 默认值:static 没有添加额外属性的relative和 ...

  6. 对于一个段错误(核心已转储)问题的解答,错误的英文翻译是segment fault(core dumped)

    笔者在学习ROS的时候遇到的这个问题,使用的系统是ubuntu16.04,ROS版本是kinetic,在运行小海龟程序的时候突然打不开海龟界面的程序节点turtlesim-node,四处寻找答案未果, ...

  7. in条件后面有多个字段,in后面只能有一个字段 Operand should contain 1 column(s)

    今天在sql测试的时候发现了这个错误:Operand should contain 1 column(s). 原因是in条件后面有多个字段,in后面只能有一个字段.

  8. 第一次scrum冲刺

     一.第一次冲刺任务          首先分工做好全局规划,然后基于规划实现全部功能,当然现在只是部分.   二.用户故事        用户进入界面    用户输入账号密码        不记得密 ...

  9. C# 保存屏幕截图

    //屏幕宽 int iWidth = Screen.PrimaryScreen.Bounds.Width; //屏幕高 int iHeight = Screen.PrimaryScreen.Bound ...

  10. Classnotfoundexception 与 noClassDelfaultError的区别

    ClassNotFoundException 这个异常特别常见,就是class找不到异常,一般的问题就是: 1 调用class的forName方法时,找不到指定的类 2 ClassLoader 中的 ...