假设有一个容器中存放着 int ,Container<int> c, 现在想从其中删除数值 1963,可以有如下方法:

1: c.erase(remove(c.begin(), c.end(), 1963), c.end()); // c is continguous memory container
2: c.remove(1963); // c is list
3: c.erase(1963) // c is standard associative container

对于 continguous memory container 来说, std::remove() 可以保证返回的 iterator 之前的内存中不包含被删除的值,配合 erase 来用,形成所谓的 erase-remove idiom 。而对于 List 来说,应该调用 list 本身的 remove() 方法。对于 map,set 等等,则必须使用 erase() 方法,而不能使用 remove() ,对 associative container 使用 remove() 可能会覆盖容器存储的内容,甚至破坏容器。

假设我们现在想给定一个条件( bool badValue(int x) )并用这个条件来判断有哪些 Entry 需要删除,对 vector、string、deque、list 可以用下列方法:

1: c.erase(remove_if(c.begin(), c.end(), badValue), c.end()); // for vector, string, deque ...
2: c.remove_if(badValue); // for list.

associative container 没有类似的方法,但可以自行遍历删除:

AssocContainer<int> c;
for (AssocContainer<int>::iterator i=c.begin(); i != c.end(); ++i)
{
if (badValue(*i))
{
c.erase(i); //XXX: This is wrong: when this get executed, iterator will become
//XXX: invalid, and ++i will lead to undefined behaviour.
}
} for (AssocContainer<int>::iterator i=c.begin(); i != c.end(); /*Nothing*/)
{
if (badValue(*i))
{
c.erase(i++); //XXX: This works, because we are passing unincreased value to erase,
//XXX: but we get it increased before it was passed to erase().
}
else
{
i++;
}
}
 

Effective STL 笔记 -- Item 9: Choose carefully among erasing options的更多相关文章

  1. Effective STL 笔记 -- Item 6 ~ 7: Container and Object Pointer

    Effective STL 笔记 – Item 6 ~ 7: Container and Object Pointer 中间两次笔记被删掉了,简单补一下: Item 3 中提到如果将对象直接放入容器中 ...

  2. Effective STL 笔记: Item 6--Be alert for C++'s most vexing parse

    假设有个文件里面记录的一系列的 int 值,现在我们想把这些数值存到一个 List 里面,结合 Item 5, 我们可能会写出下面的代码: ifstream dataFile("ints.d ...

  3. C++学习书籍推荐《Effective STL(英文)》下载

    百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing train ...

  4. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  5. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  6. Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value

    Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...

  7. Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据

    Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...

  8. Effective STL 学习笔记 Item 30: 保证目标区间足够大

    Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...

  9. Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor

    Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...

随机推荐

  1. python eval 用法

    eval 功能:将字符串str当成有效的表达式来求值并返回计算结果. 语法: eval(source[, globals[, locals]]) -> value 参数: source:一个Py ...

  2. 队列,event,multiprocess

    队列:queue queue is especially useful in threaded programming when information must be exchanged safel ...

  3. Oracle数据库代码指令简介

    重大提醒!!!oracle里面的查询,一定要把查询名大写!!!就算你创建的时候是小写字母,查询的时候也一定要大写!!! 这是oracle的课后作业,弄懂这些也差不多了吧,不懂的可以去看我的SQL se ...

  4. Fireworks基本使用

    新建文件:ctrl+n 打开:ctrl+o 调出和隐藏标尺:ctrl+r 清除辅助线:视图.辅助线.清除辅助线 放大镜:z:放大镜状态下alt+左键:缩小 抓手:快捷键 测量距离:先拉出2根辅助线,然 ...

  5. MongoDB - Introduction to MongoDB, Databases and Collections

    MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...

  6. shell逐行读取excel并执行sql

    背景:测试同学想根据一组user_id 获取token做测试,由于各种原因(数据量大,sql语句复杂),只能根据user_id一条条查,于是写了个脚本: 1, 先查询需要的user_id 导出到use ...

  7. kafka入门(2)- 环境部署

    部署Zookeeper(单机/集群) 1.下载安装文件: http://mirror.bit.edu.cn/apache/zookeeper/ 2.解压文件(本文解压到 D:\zookeeper-3. ...

  8. python collection 中的队列

    认识中的队列 在以前的认知里,队列是先进先出,就是一头进,一头出,Queue.而无意间看到了deque 双向队列. 即从该队列的头或者尾部都能插入和移除元素.而起时间复杂度竟然是一样的!O(1),是不 ...

  9. 20165227朱越 预备作业3 Linux安装及学习

    预备作业3 Linux安装及学习 Linux的安装 虚拟机的安装远没有想象中的那样容易,下载还没有出现什么问题,当我安装的时候,第一个问题出现在创建虚拟机时选择安装的虚拟机版本和类型的时候的错误 当时 ...

  10. 子查询优化--explain与profiling分析语句

    今天想的利用explain与progiling分析下语句然后进行优化.本文重点是如何通过explain与profiling分析SQL执行过程与性能.进而明白索引的重要性. 表的关系如下所示: 原始的查 ...