上篇将了对于struct或是class为何emplace_back要优越于push_back,可是另一些细节没有提及。今天就谈一谈emplace_back造成的引用失效。

直接撸代码了:

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> ivec;
  8. ivec.emplace_back(1);
  9. ivec.emplace_back(ivec.back());
  10. for (auto it = ivec.begin(); it != ivec.end(); ++it)
  11. cout << *it << " ";
  12. return 0;
  13. }
  14. //输出:
  15. 1 -572662307

尝试1:不直接给emplace_back传递ivec.back():

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> ivec;
  8. ivec.emplace_back(1);
  9. auto &it = ivec.back();
  10. ivec.emplace_back(it);
  11. for (auto it = ivec.begin(); it != ivec.end(); ++it)
  12. cout << *it << " ";
  13. return 0;
  14. }
  15. 输出:
  16. 1 -572662307

尝试2:不给emplace_back传递引用:

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> ivec;
  8. ivec.emplace_back(1);
  9. auto it = ivec.back();
  10. ivec.emplace_back(it);
  11. for (auto it = ivec.begin(); it != ivec.end(); ++it)
  12. cout << *it << " ";
  13. return 0;
  14. }
  15. 输出:
  16. 1 1

我们如愿以偿,这时候应该能够得到结论了,ivec.back()返回的是引用,可是这个引用失效了,所以才会输出不对;我们之前也提到过,又一次分配内存会造成迭代器的失效,这里是造成了引用的失效。

再回头看看emplace_back的描写叙述:

if a reallocation happens, all iterators, pointers and references related to this container are invalidated.

Otherwise, only the end iterator is invalidated, and all other iterators, pointers and references to elements are guaranteed to keep referring to the same elements they were referring to before the call.

进一步。

尝试3:避免emplace_back引起又一次分配内存:

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> ivec;
  8. ivec.reserve(4);
  9. ivec.emplace_back(1);
  10. ivec.emplace_back(ivec.back());
  11. for (auto it = ivec.begin(); it != ivec.end(); ++it)
  12. cout << *it << " ";
  13. return 0;
  14. }
  15. 输出:
  16. 1 1

可是这个时候问题来了,假设不使用emplace_back而改用push_back呢?

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> ivec;
  8. ivec.push_back(1);
  9. ivec.push_back(ivec.back());
  10. ivec.push_back(ivec.back());
  11. ivec.push_back(ivec.back());
  12. for (auto it = ivec.begin(); it != ivec.end(); ++it)
  13. cout << *it << " ";
  14. return 0;
  15. }
  16. //输出:
  17. 1 1 1 1

为什么使用push_back就不失效呢?

实战c++中的vector系列--emplace_back造成的引用失效的更多相关文章

  1. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  2. 实战c++中的vector系列--知道emplace_back为何优于push_back吗?

    上一篇博客说道vector中放入struct.我们先构造一个struct对象.再push_back. 那段代码中,之所以不能使用emplace_back,就是由于我们定义的struct没有显示的构造函 ...

  3. 实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

    使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中. find() Returns an iterator to the first element ...

  4. 实战c++中的vector系列--将迭代器转换为索引

    stl的迭代器非常方便 用于各种算法. 可是一想到vector.我们总是把他当做数组,总喜欢使用下标索引,而不是迭代器. 这里有个问题就是怎样把迭代器转换为索引: #include <vecto ...

  5. 实战c++中的vector系列--构造、operator=和assign差别

    vector或许是实际过程中使用最多的stl容器.看似简单,事实上有非常多技巧和陷阱. 着重看一看vector的构造,临时依照C++11: default (1) explicit vector (c ...

  6. 实战c++中的vector系列--creating vector of local structure、vector of structs initialization

    之前一直没有使用过vector<struct>,如今就写一个简短的代码: #include <vector> #include <iostream> int mai ...

  7. 实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit())

    关于vector已经写的差不多了,似乎要接近尾声了,从初始化到如何添加元素再到copy元素都有所涉及,是时候谈一谈内存的释放了. 是的,对于数据量很小的vector,完全没必要自己进行主动的释放,因为 ...

  8. 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)

    stl算法中有个copy函数.我们能够轻松的写出这种代码: #include <iostream> #include <algorithm> #include <vect ...

  9. 实战c++中的vector系列--vector&lt;unique_ptr&lt;&gt;&gt;初始化(全部权转移)

    C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ...

随机推荐

  1. 合并百度影音的离线数据 with python 2.1 bdv格式的更新

    最近百度影音的离线下载文件,格式有新变化. 经过分析,是bdv格式又有新格式,从最初的bdv0001,到bdv.config 的file....,这次更新的格式是直接包含一个片段,其中还有使用guid ...

  2. 生产者-消费者中的缓冲区:BlockingQueue接口

    BlockingQueue接口使用场景相信大家对生产者-消费者模式不陌生,这个经典的多线程协作模式,最简单的描述就是生产者线程往内存缓冲区中提交任务,消费者线程从内存缓冲区里获取任务执行.在生产者-消 ...

  3. hibernate fetch属性

    fetch的属性值有:select(默认值).join.subselect 1)当fetch=”select”时,程序会先查询返回要查询的主体对象,然后根据lazy属性看是否懒加载. 2)当fetch ...

  4. CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

    Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机 ...

  5. ubuntu12.04 配置apache+modwsgi+django1.5

    1.首先下载modwsgi  链接如下: http://files.cnblogs.com/baoyiluo/mod_wsgi-3.4.zip 2.解压并安装mod_wsgi: ./configure ...

  6. 基于短语的统计机器翻(PBMT) 开源工具 :Moses

    如何运行Moses 1. Moses的历史 Moses是Pharaoh的升级版本,增加了许多功能.它是一个基于短语的统计机器翻译系统,整个系统用C++语言写成,从训练到解码完全开放源代码,可以运行在L ...

  7. iOS缓存到内存

    前面一片文章介绍了如何上传和下载文件,这篇文章将介绍一下如何在iOS设备中进行缓存. 这篇文章将只介绍一下将内容缓存到内存中,下一篇文章就介绍一下在iOS磁盘上缓存内容. 使用缓存的目的是为了使用的应 ...

  8. 简谈Redis

    1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...

  9. [Python3网络爬虫开发实战] 1.4.3-Redis的安装

    Redis是一个基于内存的高效的非关系型数据库,本节中我们来了解一下它在各个平台的安装过程. 1. 相关链接 官方网站:https://redis.io 官方文档:https://redis.io/d ...

  10. 读书笔记:《人有人的用处》------N.维纳. (2016.12.28)

    读书笔记:<人有人的用处>------N.维纳 ·某些系统可以依其总能量而和其他系统区别开来. ·在某些情况下,一个系统如果保持足够长时间的运转,那它就会遍历一切与其能量相容的位置和动量的 ...