Effective STL 学习笔记 32 ~ 33

*/-->

div.org-src-container {
font-size: 85%;
font-family: monospace;
}
pre.src {
background-color:#f8f4d7
}
p {font-size: 15px}
li {font-size: 15px}

1 记得 Remove 后要 Erase

Item 9 中已经提到过,如果真的想要用 remove 从容器中删除元素,必须要在 remove 之后使用 erase
。其原因在于: remove 接受的参数是通用的迭代器,而不是容器,它不知道容器的任何信息,也就无法从容器中删除元素: remove doesn't really remove anything, because it can't.

remove 只保证在其返回的迭代器之前的元素为有效数据(符合条件的数据),但它既无法删除被 remove
的数据,也不能保证返回的迭代器之后的数据位无效数据(不符合条件的数据)。这一点与某些容器内置的 remove member function 不一样:这些 member function 会真正的把数据删除掉:

// For vector, we have to call erase after remove.
vector<int> v;
// ....
vector<int>::iterator endPos = remove(v.begin(), v.end(), 99); // Remove 99 from vector, returns last valid position.
v.erase(posEnd, v.end()); // Erase all elements after posEnd. // For list, the member function remove is enough:
list<int> l;
//...
l.remove(99); // this will erase all 99 in the list.

2 remove, container, pointer

Item 6 中提到,在容器中放置对象指针容易混乱,这里如果对存放指针的容器调用 remove-erase idom
的话,会发生什么事情?

 1: class Widget
2: {
3: public:
4: Widget();
5: virtual ~Widget();
6: bool IsCertified();
7: };
8:
9: vector<Widget*> v;
10:
11: // push lots of widget pointers to v.
12:
13: v.erase(remove(v.begin(), v.end(), not1(mem_fun(&Widget::IsCertified))), v.end());

内存泄露。

那么将第 13 行换成下面的表达式呢?

14: vector<Widget*> endPos = remove(v.begin(), v.end(), not(mem_fun(&Widget::IsCertified)));
15: for_each(endPos, v.end(), [](Widget* pw){if(pw) delete pw;});
16: v.erase(endPos, v.end());

第 14 行将所有的 endPos 之后的指针先释放,然后再去 erase。前面刚刚提过, remove 不会保证返回的迭代器之后的元素都为无效值,第 14 行有可能会将本该保留的对象给释放掉,还有可能会将该释放的不释放。结果可能会 Crash 或者内存泄露。

我们应该保证在 remove 过程中,一旦发现不合要求的数据,马上将其删除,例如下面的例子:

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
#include <stdio.h> using namespace std; class Widget
{
public:
Widget(int i);
virtual ~Widget();
bool IsCertified();
void Show();
private:
int m_i;
}; /* See description in header file. */
Widget::Widget(int i)
: m_i(i)
{
} /* See description in header file. */
Widget::~Widget()
{
printf ("Deleting Obj: %p, value: %d\n", this, m_i);
} /* See description in header file. */
bool Widget::IsCertified()
{
return m_i % 2 == 0;
} /* See description in header file. */
void Widget::Show()
{
printf ("\tObj: %p, value: %d\n", this, m_i);
} bool DeleteIfUncitified(Widget* p)
{
if (p && !p->IsCertified())
{
delete p;
return true;
}
return false;
} int main(int argc, char *argv[])
{
vector<Widget*> v;
for (int i = 0; i < 20; ++i)
{
Widget* p = new Widget(i);
v.push_back(p);
} random_shuffle(v.begin(), v.end()); printf ("Before remove, size: %lu, contents:\n", v.size());
for_each(v.begin(), v.end(),
[](Widget* p){
if (p) p->Show();
else printf("Obj is Null");});
printf ("\nNow removing...\n");
v.erase(remove_if(v.begin(), v.end(), DeleteIfUncitified), v.end());
printf ("\nAfter remove, size: %lu, contents:\n", v.size());
for_each(v.begin(), v.end(),
[](Widget* p){
if (p) p->Show();
else printf("Obj is Null");}); return 0;
}

其输出为:

Welcome to the Emacs shell

~/tmp $ ./test
Before remove, size: 20, contents:
Obj: 0x7f8b31c038d0, value: 19
Obj: 0x7f8b31c03870, value: 3
Obj: 0x7f8b31c039e0, value: 14
Obj: 0x7f8b31c039f0, value: 15
Obj: 0x7f8b31c03890, value: 10
Obj: 0x7f8b31c03850, value: 2
Obj: 0x7f8b31c03900, value: 6
Obj: 0x7f8b31c038b0, value: 17
Obj: 0x7f8b31c03920, value: 8
Obj: 0x7f8b31c038a0, value: 4
Obj: 0x7f8b31c039c0, value: 12
Obj: 0x7f8b31c03910, value: 7
Obj: 0x7f8b31c038f0, value: 5
Obj: 0x7f8b31c039b0, value: 11
Obj: 0x7f8b31c03860, value: 1
Obj: 0x7f8b31c03880, value: 9
Obj: 0x7f8b31c03840, value: 0
Obj: 0x7f8b31c03a00, value: 16
Obj: 0x7f8b31c039d0, value: 13
Obj: 0x7f8b31c038c0, value: 18 Now removing...
Deleting Obj: 0x7f8b31c038d0, value: 19
Deleting Obj: 0x7f8b31c03870, value: 3
Deleting Obj: 0x7f8b31c039f0, value: 15
Deleting Obj: 0x7f8b31c038b0, value: 17
Deleting Obj: 0x7f8b31c03910, value: 7
Deleting Obj: 0x7f8b31c038f0, value: 5
Deleting Obj: 0x7f8b31c039b0, value: 11
Deleting Obj: 0x7f8b31c03860, value: 1
Deleting Obj: 0x7f8b31c03880, value: 9
Deleting Obj: 0x7f8b31c039d0, value: 13 After remove, size: 10, contents:
Obj: 0x7f8b31c039e0, value: 14
Obj: 0x7f8b31c03890, value: 10
Obj: 0x7f8b31c03850, value: 2
Obj: 0x7f8b31c03900, value: 6
Obj: 0x7f8b31c03920, value: 8
Obj: 0x7f8b31c038a0, value: 4
Obj: 0x7f8b31c039c0, value: 12
Obj: 0x7f8b31c03840, value: 0
Obj: 0x7f8b31c03a00, value: 16
Obj: 0x7f8b31c038c0, value: 18
~/tmp $

Effective STL 学习笔记 32 ~ 33的更多相关文章

  1. Effective STL 学习笔记 39 ~ 41

    Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  2. 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 ...

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

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

  4. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

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

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

  6. 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 ...

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

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

  8. Effective STL 学习笔记 Item 21:Comparison Function 相关

    Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...

  9. Effective STL 学习笔记:19 ~ 20

    Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

随机推荐

  1. Android dumpsys命令详细使用

    一.dumpsys命令介绍 1.命令说明 Dumpsys用户系统诊断,它运行在设备上,并提供系统服务状态信息 命令格式: adb shell dumpsys [system serbices] 2.系 ...

  2. 报错laravel/horizon v1.4.3 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system.的解决办法

    执行composer install后报以下错误: Problem 1 - laravel/horizon v1.4.3 requires ext-pcntl * -> the requeste ...

  3. 执行ldconfig命令后报错的解决过程:ldconfig: 目录 /lib 中的 libpng.so 和 libpng15.so.15.13.0 的 so 名称相同但类型不同。

    执行ldconfig命令后报错: 目录 /lib 中的 libpng.so 和 libpng15.so.15.13.0 的 so 名称相同但类型不同. 解决过程: mv /lib/libpng.so ...

  4. MongoDB 安装及开启关闭

    开启关闭的方式: 命令行 输入 net start mongodb 就打开mongo的服务了 输入 net stop mongodb 关闭服务 验证是否成功的方式: 在浏览器中输入 http://lo ...

  5. [Luogu 3275] SCOI2011 糖果

    [Luogu 3275] SCOI2011 糖果 第一道差分约束.感谢 AZe. 我的理解是根据一些不等关系建一个图,在图上边跑一个最长路(有时候是最短路). 因为可能存在负环,所以必须用 SPFA! ...

  6. [转载].net程序集自动生成版本号

    原文:http://hi.baidu.com/bcbgrand/item/a74a7ba71c3b0ea928ce9dce .net程序版本号的格式是4个十进制数字 比如 2.5.729.2 依次是 ...

  7. jquery中美元符号命名冲突问题解决

    在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替换,如$('#msg')等同于JQuery('#msg') 的写法.然而,当我们引入多个js库后,在另外一个js库 ...

  8. 平铺式窗口管理器 Musca 初体验

    作者: 吴吉庆 Version: 1.0 release: 2009-11-04 update: 2009-11-04 为什么用平铺式窗口管理器? 什么是平铺式窗口管理器(tiling window ...

  9. [洛谷P1228]地毯填补问题 题解(分治)

    Description 相传在一个古老的阿拉伯国家里,有一座宫殿.宫殿里有个四四方方的格子迷宫,国王选择驸马的方法非常特殊,也非常简单:公主就站在其中一个方格子上,只要谁能用地毯将除公主站立的地方外的 ...

  10. AAC编码

    1. 前言 如果说目前H.264是视频CODEC的实际霸主,那么AAC就是音频CODEC的女王.主流的音视频格式都是H.264搭配AAC,无论是非实时的媒体文件还是实时的媒体流. 2. AAC历史 A ...