实战c++中的vector系列--构造、operator=和assign区别
来源:互联网 发布:最好的大六壬排盘软件 编辑:程序博客网 时间:2024/05/30 23:28
vector也许是实际过程中使用最多的stl容器,看似简单,其实有很多技巧和陷阱。
着重看一看vector的构造,暂时按照C++11:
default (1) explicit vector (const allocator_type& alloc = allocator_type());fill (2) explicit vector (size_type n); vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());range (3) template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());copy (4) vector (const vector& x);vector (const vector& x, const allocator_type& alloc);move (5) vector (vector&& x);vector (vector&& x, const allocator_type& alloc);initializer list (6) vector (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());
直接看看下面的代码,就知道如何构造一个vector了:
#include <iostream>#include <vector>int main (){ std::vector<int> first; // default (1) std::vector<int> second (4,100); // fill (2) std::vector<int> third (second.begin(),second.end()); // range (3) std::vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); std::cout << "The contents of fifth are:"; for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}
===========================================================
vector重载了=运算符,也有一个叫assign的方法,而且有什么区别吗?
std::vector::operator=
直接代码:
#include <iostream>#include <vector>int main (){ std::vector<int> foo (3,0); std::vector<int> bar (5,0); bar = foo; foo = std::vector<int>(); std::cout << "Size of foo: " << int(foo.size()) << '\n'; std::cout << "Size of bar: " << int(bar.size()) << '\n'; return 0;}//结果:Size of foo: 0Size of bar: 3
这里需要说明的是:
replacing its current contents
modifying its size accordingly
std::vector::assign
同样直接代码:
#include <iostream>#include <vector>int main (){ std::vector<int> first; std::vector<int> second; std::vector<int> third; first.assign (7,100); // 7 ints with a value of 100 std::vector<int>::iterator it; it=first.begin()+1; second.assign (it,first.end()-1); // the 5 central values of first int myints[] = {1776,7,4}; third.assign (myints,myints+3); // assigning from array. std::cout << "Size of first: " << int (first.size()) << '\n'; std::cout << "Size of second: " << int (second.size()) << '\n'; std::cout << "Size of third: " << int (third.size()) << '\n'; return 0;}//输出:Size of first: 7Size of second: 5Size of third: 3
这里同样需要说明:
replacing its current contents
modifying its size accordingly
1 1
- 实战c++中的vector系列--构造、operator=和assign区别
- 实战c++中的vector系列--assign() push_back() or insert()
- 实战c++中的vector系列--使用vector构造二维数组
- 拷贝构造和operator=的区别
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中判断不等于end())、operator[])
- 关于vector中的assign和resize函数
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 实战c++中的vector系列--copy set to vector(别混淆了reserve和resize)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- 实战c++中的vector系列--对vector<自定义类>使用std::find 和 std::find_if 算法
- 实战c++中的智能指针unique_ptr系列-- unique_ptr的operator=、operator bool、reset、swap、get等介绍
- 实战c++中的vector系列--vector的一些异常
- 关于copy构造函数和operator=
- C++拷贝构造函数和operator=
- 复制构造函数和operator=
- operator++()和operator++(int) 区别
- vector::insert, vector::max_size, vector::operator=
- UI06_UIPageControl
- 使用FreeImage写通用图像加载器
- E-R图设计的步骤
- pAdTy_4 构建有联系人和签署的应用程序
- DOM 对象控制 HTML
- 实战c++中的vector系列--构造、operator=和assign区别
- Valid Parentheses
- Unity3D 把代码混淆过的代码返混淆回来
- LogCat中没有信息显示:device disconnected:1
- JS代码整理
- FreeImage通用文件加载与转换器
- AndroidStudio导出Jar包
- AdaGrad理解错误更正
- ps_3_0 does not allow textures or samplers to be members of compound types