vector或许是实际过程中使用最多的stl容器。看似简单,事实上有非常多技巧和陷阱。

着重看一看vector的构造,临时依照C++11:

  1. default (1)
  2. explicit vector (const allocator_type& alloc = allocator_type());
  3. fill (2)
  4. explicit vector (size_type n);
  5. vector (size_type n, const value_type& val,
  6. const allocator_type& alloc = allocator_type());
  7. range (3)
  8. template <class InputIterator>
  9. vector (InputIterator first, InputIterator last,
  10. const allocator_type& alloc = allocator_type());
  11. copy (4)
  12. vector (const vector& x);
  13. vector (const vector& x, const allocator_type& alloc);
  14. move (5)
  15. vector (vector&& x);
  16. vector (vector&& x, const allocator_type& alloc);
  17. initializer list (6)
  18. vector (initializer_list<value_type> il,
  19. const allocator_type& alloc = allocator_type());

直接看看以下的代码,就知道怎样构造一个vector了:

  1. #include <iostream>
  2. #include <vector>
  3. int main ()
  4. {
  5. std::vector<int> first; // default (1)
  6. std::vector<int> second (4,100); // fill (2)
  7. std::vector<int> third (second.begin(),second.end()); // range (3)
  8. std::vector<int> fourth (third); // a copy of third
  9. // the iterator constructor can also be used to construct from arrays:
  10. int myints[] = {16,2,77,29};
  11. std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
  12. std::cout << "The contents of fifth are:";
  13. for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
  14. std::cout << ' ' << *it;
  15. std::cout << '\n';
  16. return 0;
  17. }

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

vector重载了=运算符,也有一个叫assign的方法,并且有什么差别吗?

std::vector::operator=

直接代码:

  1. #include <iostream>
  2. #include <vector>
  3. int main ()
  4. {
  5. std::vector<int> foo (3,0);
  6. std::vector<int> bar (5,0);
  7. bar = foo;
  8. foo = std::vector<int>();
  9. std::cout << "Size of foo: " << int(foo.size()) << '\n';
  10. std::cout << "Size of bar: " << int(bar.size()) << '\n';
  11. return 0;
  12. }
  13. //结果:
  14. Size of foo: 0
  15. Size of bar: 3

这里须要说明的是:

replacing its current contents

modifying its size accordingly

std::vector::assign

相同直接代码:

  1. #include <iostream>
  2. #include <vector>
  3. int main ()
  4. {
  5. std::vector<int> first;
  6. std::vector<int> second;
  7. std::vector<int> third;
  8. first.assign (7,100); // 7 ints with a value of 100
  9. std::vector<int>::iterator it;
  10. it=first.begin()+1;
  11. second.assign (it,first.end()-1); // the 5 central values of first
  12. int myints[] = {1776,7,4};
  13. third.assign (myints,myints+3); // assigning from array.
  14. std::cout << "Size of first: " << int (first.size()) << '\n';
  15. std::cout << "Size of second: " << int (second.size()) << '\n';
  16. std::cout << "Size of third: " << int (third.size()) << '\n';
  17. return 0;
  18. }
  19. //输出:
  20. Size of first: 7
  21. Size of second: 5
  22. Size of third: 3

这里相同须要说明:

replacing its current contents

modifying its size accordingly

实战c++中的vector系列--构造、operator=和assign差别的更多相关文章

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

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

  2. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

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

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

  4. 实战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 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 原生php如何获取当前页面的url

    原生php如何获取当前页面的url? //php获取当前访问的完整url地址 function get_current_url(){ $current_url='http://'; if(isset( ...

  2. PHP原生实现,校验微信公众号||小程序服务器地址

    1.原生的.php文件:  test.php <?php header('Content-type:text'); define("TOKEN", "weixin& ...

  3. C#利用反射机制调用dll

    利用反射进行动态加载和调用. Assembly ass=Assembly.LoadFrom(DllPath); //利用dll的路径加载,同时将此程序集所依赖的程序集加载进来,需后辍名.dll Ass ...

  4. 关于iOS 类扩展Extension的进一步理解

    很多人可能会问  iOS的分类和扩展的区别,网上很多的讲解,但是一般都是分类讲的多,而这也是我们平常比较常用的知识:但是,对于扩展,总觉得理解的朦朦胧胧,不够透彻. 这里就讲一下我自己的理解,但是这个 ...

  5. HDUOJ---2082

    找单词 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 【LeetCode】52. N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  7. iOS - App 上架审核被原因拒总结

    1.未遵守苹果 iOS APP 数据储存指导方针 如果你的 App 有离线数据下载功能,尤其需要关注这一点.因为离线数据一般占用存储空间比较大,可以被重新下载和重建,但是用户往往希望系统存储空间紧时也 ...

  8. bootstraptable toolbar

  9. Python 之ConfigParser 学习笔记

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  10. 套接字I/O超时设置方法和用select实现超时

    注:如无特殊说明,sockfd 原始状态都是阻塞的. 一.使用alarm 函数设置超时  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13   void handler( ...