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. 命令:curl

    在227服务器上执行 curl -i -X PUT --url http://192.168.1.227:8001/apis/ --data 'name=getweather' --data 'ups ...

  2. css引用与html语义化

    一.CSS引用1. 使用外部样式表:    CSS代码在一个独立的文件中,HTML通过link元素引入到页面 格式:link + tab键<link rel="stylesheet&q ...

  3. linux安装mysql(shell一键安装)

    1. 相关文件(install_mysql.sh.my.cnf.mysqld相关内容在文中最后面) 2. 将上面的文件上传到linux服务器某一目录下 3.给install_mysql.sh赋执行权限 ...

  4. JAVA Character类

    字符可以用char类型声明: char ch = 'a'; // Unicode 字符表示形式 char uniChar = '\u039A'; // 字符数组 char[] charArray ={ ...

  5. DevExpress v18.2新版亮点——DevExtreme篇(四)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExtreme Complete Sub ...

  6. L1-037 A除以B (10 分)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805094485180416   真的是简单题哈 —— 给定两个 ...

  7. Python Opencv安装环境搭建

    https://blog.csdn.net/weifenglin1997/article/details/78723544

  8. Linux系统调用列表(转)

    以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的Linux系统调用列表,即使是简单的字母序英文列表,能做到这么完 ...

  9. wifi编辑 centos

    ifconfig -a sudo iw dev 设置名称 scan

  10. privacy policy url

    提交审核资料时需要给出隐私条约资料网址privacy policy url 参考新浪微博地址http://m.weibo.cn/page/646?entry=client