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: 0
Size 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: 7
Size of second: 5
Size of third: 3

这里相同须要说明:

replacing its current contents

modifying its size accordingly

最新文章

  1. Linux下取代top的进程管理工具 htop
  2. 开发工程师面试的秘密( 整理自 Export C Programming )
  3. 如何解读SQL Server日志(3/3)
  4. 从零开始学Python04作业思路:模拟ATM电子银行
  5. Android菜鸟成长记4-button点击事件
  6. jQuery中position()与offset()区别
  7. 严重: Exception starting filter struts2解决方法!
  8. Mac系统之----教你怎么显示隐藏文件,或者关闭显示隐藏文件
  9. how to reset mac root password
  10. 使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务
  11. 【HDOJ】4579 Random Walk
  12. SQL第二课-创建数据表
  13. Eclipse代码提示功能设置(Java &amp; Eclipse+CDT C/C++)
  14. hdu 4856 Tunnels(bfs+状态压缩)
  15. 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]
  16. VS2003&quot;无法启动调试 没有正确安装调试器&quot;的解决方法
  17. Jmeter + Ant 测试环境搭建 及解决问题: the &lt;jmeter&gt; type doesn&#39;t support nested text data
  18. ansible的Filter
  19. windows下面Nginx日志切割
  20. Entity Framework 6 自定义连接字符串ConnectionString连接MySQL

热门文章

  1. CAS连接微软活动目录的配置方法
  2. nginx1.9+新增tcp/udp代理stream
  3. 关于iOS Category实现添加属性及成员变量
  4. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
  5. Android的各版本间的区别总结
  6. Google官方下拉刷新组件---SwipeRefreshLayout
  7. Linux命令-安全复制命令:scp
  8. Android 方向传感器
  9. Python练习笔记——利用递归求年龄,第五个比第四个大2岁...
  10. Linux内核同步 - RCU synchronize原理分析