与其它动态序列容器相比(deques, lists and forward_lists), vector在访问元素的时候更加高效,在末尾添加和删除元素相对高效,对于其他不在末尾的删除和插入操作,相率更低。
1.2 vector的使用
1.2.1 vector的定义
构造函数声明
接口说明
vector()
无参构造
vector(size_type n, const value_type& val = value_type())
构造并初始化n个val
vector (const vector& x);
拷贝构造
vector (InputIterator first, InputIterator last);
使用迭代器进行初始化构造
// constructing vectors#include<iostream>#include<vector>intmain(){
// constructors used in the same order as described above:
std::vector<int> first;// empty vector of ints
std::vector<int>second(4,100);// four ints with value 100
std::vector<int>third(second.begin(), second.end());// iterating through second
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';return0;}