参考文档: http://www.cplusplus.com/reference/vector/vector/
属性
连续存储的动态改变大小的数组
方法
初始化型
构造函数
- 默认构造函数
- 填充构造fill
- 迭代器范围复制构造range
- 复制构造copy
- 由数组构造
// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// 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
// construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
return 0;
}
重载赋值运算符 =
迭代器类型
begin(),end()
rbegin(), rend()
cbegin(), cend()
crbegin(), crend()
r表示reverse倒置,c表示const常量,c在C++11中有效
容器(常用,详细见参考文档)
size() vector大小
resize(n),resize(n,val) 改变vector大小,删后补val(默认0)
empty() 判空
元素获取
索引运算符[]
at(pos)
front() 第一个元素
back() 最后一个元素
data() 第一个元素的指针
元素修改
push_back()、pop_back()
assign(,) range方式指派、fill方式指派
insert(position, ,) 插入单个元素、range方式插入、fill方式插入(position为iterator类型)
erase(position,) 删除单个元素、range方式删除元素(均为iterator类型)
emplace(postion,val) 在指定位置插入一个新元素
vector修改
swap(a) 交换两个vector内容
clear() 清空vector