1. vector<int> first;//Size()==2
  2. first.push_back();
  3. first.push_back();
  4. //first.insert(2);
  5.  
  6. vector<int>second;//Size()==3 + assign??
  7. second.push_back();
  8. second.push_back();
  9. second.push_back();
  10. vector<int>::iterator it = first.begin();
  11. //second.assign(it,first.end());
  12. second.insert(second.end(),first.begin(),first.end());
  13.  
  14. printf("first.size=%d\n", first.size());
  15. printf("second.size=%d\n", second.size());
  16. for (it = second.begin(); it != second.end(); it++)
  17. printf("second[]=%d\n", *it);

second 要插入first的内容,太长了。好麻烦。怎么就没有直接 push_back(vector) 的 重构函数呢??

emplace 是C++11 里的 ,感觉跟这个 insert用法是一样的啊。

assign 是完全替换的意思。 直接用 operator = 难道不就OK了么?

reserve是修改vector目前数组 的大小。配套的是capacity()方法。获取数组的大小。

resize是修改vector 内容的。配套的是size()方法。获取数据的个数。 size() <= capacity();

--------------

真想把 www.cplusplus.com 这个网站给抓下来,然后自己做一个站点。

它没有评论;有些示例 非常不好。

补充 vector 进行iterator 且 erase删除元素的注意事项

==============================

【1】 首先,了解vector是数组,删除元素 会把后面的元素前移。

for( iterator it =XX.begin();it!=XX.end();it++){...}

【2】这里的it++ 只是 从前一个数组元素 移动到后一个数组元素,一般都是比较连续的地址,跟 vector<>里面的元素大小有关。

【3】而且for循环中 it!=XX.end() 这个条件 是一个坑,因为 它 不是实时的,当元素删除后,XX.end() 的值(地址) 应该会变动,但是for循环中 不会。所以 for循环 中间的条件 最好 在 for循环内 代码区域 进行判断,则是实时的【我的描述能力实在有限,只能这么说了】。

【4】可以使用 reverse_iterator 配合 erase 进行删除就安全一点

下面给一个例子:

  1. #include <stdio.h>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. void iteratorVector(vector <int>&vInts){
  6. int i = ;
  7. for (vector<int>::iterator it = vInts.begin(); it != vInts.end(); it++){
  8. printf("\t[%d] => %d\n", i++, *it);
  9. }
  10. printf("===== End Iterator Vector ====\n");
  11. }
  12.  
  13. void test_erase(vector<int>&vInts, int nId)
  14. {
  15. for (vector<int>::iterator it = vInts.begin(); it != vInts.end(); it++){
  16. if (*it == nId){
  17. it = vInts.erase(it);
  18. return;
  19. }
  20. }
  21. }
  22.  
  23. void test_reverse_iterator_vector(vector<int> & vInts)
  24. {
  1. //这里的 rbegin 应该 和 iterator 的end() 类似;
  2. //这里的 rend 应该 和 iterator 的 begin() 类似;
  1. for (vector<int>::reverse_iterator it = vInts.rbegin(); it != vInts.rend(); it++){
  1. test_erase(vInts, *it);
  2. iteratorVector(vInts);
  3. }
  1. return;
  2. //==== 等效代码 ====
  3. for (vector<int>::iterator it = vInts.end();;){
  4. if (it == vInts.begin())break;
  5. it--;
  6. test_erase(vInts, *it);
  7. iteratorVector(vInts);
  8. }
  1. }
  2.  
  3. int main()
  4. {
  5. vector<int> playingUserID;
  6. playingUserID.push_back();
  7. playingUserID.push_back();
  8. playingUserID.push_back();
  9. playingUserID.push_back();
  10. playingUserID.push_back();
  11.  
  12. iteratorVector(playingUserID);
  13. test_reverse_iterator_vector(playingUserID);
  14.  
  15. }
  16. /*
  17. 运行结果:符合预期
  18. --------------
  19.  
  20. test_vector_rbegin_with_erase.obj
  21. [0] => 1
  22. [1] => 2
  23. [2] => 3
  24. [3] => 4
  25. [4] => 5
  26. ===== End Iterator Vector ====
  27. [0] => 1
  28. [1] => 2
  29. [2] => 3
  30. [3] => 4
  31. ===== End Iterator Vector ====
  32. [0] => 1
  33. [1] => 2
  34. [2] => 3
  35. ===== End Iterator Vector ====
  36. [0] => 1
  37. [1] => 2
  38. ===== End Iterator Vector ====
  39. [0] => 1
  40. ===== End Iterator Vector ====
  41. ===== End Iterator Vector ====
  42.  
  43. */

【5】如果要结合 iterator 与erase进行删除,那么要这么写代码:

  1. void test_iterator_vector(vector<int> & vInts)
  2. {
  3. for (vector<int>::iterator it = vInts.begin(); ; ){
  4. if (it == vInts.end())break;
  5.  
  6. test_erase(vInts, *it);
  7.  
  8. iteratorVector(vInts);
  9. }
  10. }
  11. /*
  12. [0] => 1
  13. [1] => 2
  14. [2] => 3
  15. [3] => 4
  16. [4] => 5
  17. ===== End Iterator Vector ====
  18. [0] => 2
  19. [1] => 3
  20. [2] => 4
  21. [3] => 5
  22. ===== End Iterator Vector ====
  23. [0] => 3
  24. [1] => 4
  25. [2] => 5
  26. ===== End Iterator Vector ====
  27. [0] => 4
  28. [1] => 5
  29. ===== End Iterator Vector ====
  30. [0] => 5
  31. ===== End Iterator Vector ====
  32. ===== End Iterator Vector ====
  33. */

这里没有了 it ++  ,因为 erase删除后,后面的元素 前移,那么 it 刚才所指向的地址 ,现在 就是后一个元素了,刚好用到,就不需要 it ++ -- 了。

顺便说下: it=vInts.erase(it); 这样 也是OK 的!!!!!

【6】 最后要说的就是 STL 中的vector 以及其他容器 都是 非线程安全的,使用 iterator 迭代器 ,和 插入元素 不进行 加锁控制,会导致严重的问题。

所以最好 自己 写一个 继承了 Mutex 功能的类的容器,使用stl 也就是这点风险了。

vector的 emplace 和 insert 以及使用vector进行iterator遍历 且 erase的时候注意事项的更多相关文章

  1. vector源码3(参考STL源码--侯捷):pop_back、erase、clear、insert

    vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷):空间分配.push_back vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 v ...

  2. STL—— 容器(vector)数据插入insert()方法 的返回值

    vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...

  3. emplace与insert的区别(C++11)

    转自时习之 C++11中大部分的容器对于添加元素除了传统的 insert 或者 pusb_back/push_front 之外都提供一个新的函数叫做 emplace. 比如如果你想要向 std::ve ...

  4. 用vector容器代替数组 ——使用数组初始化vector对象

    在C++中,我们不能用数组直接初始化另一数组,而只能创建新的数组,然后显式的把原数组的元素逐个复制给新的数组. 按照C语言中的做法: const size_t arry_size=6; int int ...

  5. 【转】vector中erase()的使用注意事项

    vector::erase():从指定容器删除指定位置的元素或某段范围内的元素 vector::erase()方法有两种重载形式 如下: iterator erase(   iterator _Whe ...

  6. C++中标准容器Vector,元素操作.insert()小结

    insert() 函数有以下三种用法: iterator insert( iterator loc, const TYPE &val );  //在指定位置loc前插入值为val的元素,返回指 ...

  7. C++中使用vector.erase()需要注意的事项

    本人菜鸟一枚.. 今天在用vector.erase()的时候,发现总是不能把应该erase掉的东西erase干净. 举个栗子: vector<int> num_vec; num_vec.p ...

  8. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  9. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

随机推荐

  1. 咏南IOCP中间件支持海量并发方案(集群)

    咏南IOCP中间件支持海量并发方案(集群) 支持D7~XE10.1.1开发 支持负载均衡,自动故障转移 可以在不停机的状态下,根据负载情况灵活增加中间件机器 中间件使用IOCP通信,单中间件支持并发数 ...

  2. eclipse里面设置JVM参数的问题

    在run----run configuration---Agruments里面设置JVM的参数:  -Xms256m   -Xmx1024m 肯定还有别的方式设置,今天就先写这一种方法.待续...

  3. 解决VMware下安装Ubuntu15不支持1920X1080分辨率的问题

    解决步骤如下: flashmx@ubuntu:~$ cvt # 192.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz Modeline -hsync +vsync ...

  4. 关于调整浏览器窗口JS

    有时候需要对浏览器窗口的大小进行元素的操控,当调整窗口大小时用window.onresize=function(){} 页面初始化window.onload=function(){} 要注意的是onr ...

  5. BZOJ3174 Tjoi2013 拯救小矮人(贪心+DP)

    传送门 Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个 ...

  6. C3P0连接池异常

    解决方案: 将c3p0.jar包换成c3p0-0.9.0.2.jar,c3p0这个包应该有bug 引用如下: com.mchange.v2.log.MLog Determines which libr ...

  7. 揭秘Sql2014新特性-tempdb性能提升

    一直以来,在高负载,复杂的生产环境中,tempdb的压力是成为整个实例瓶颈的重要因素之一.微软的工程师们也在各个版本中不断优化它的使用.到了Sql Server2014又有了新的特性使其性能得temp ...

  8. ASP.NET Web API 2 中的特性路由

    ASP.NET MVC 5.1 开始已经支持基于特性的路由(http://attributerouting.net),ASP.NET WEB API 2 同时也支持了这一特性. 启用特性路 由只需要在 ...

  9. 博客恢复更新 工作环境转移到Linux

    嗯, 回来了. 工作了, 以后学习和写博的时间只能靠挤了, 相信挤一挤总会有的.最近的一些计划: 重拾基础 玩好linux wid, 2014-04-27

  10. [WinAPI] 串口1-创建[包括: 打不开串口]

    本来是用一个USB扩展把一个USB括成4个,然后把USB转串口连接上,虽然设备管理器可以找到用SSCOM也能找到,但是用API就是打不开,最后把USB转串插在电脑的一个USB上就可以啦! #inclu ...