一、简介
跟array一样,vector是一个顺序容器。
vector<int> vct;
和array一样,vector使用一段连续的内存空间来存储他的元素。所以在访问的时候,就非常的便利,可以使用下标操作符,也可以使用迭代器。和array不一样的是,vector容器的大小可以动态变化。也就是说,可以动态的增添或者删减元素。
为了让容器可以快速响应容器大小的变化,vector容器的实际大小可能会大于其元素所占用的大小(简单的理解,在添加元素的时候,可以省去开辟新的存储空间的那段时间),所以某些情况下,测出来的size()的值和max_size()的值会有所差别。
也正是因为vector使用的是一段连续的存储空间,可以在vector尾部插入元素或者删除元素会非常的快。但是在vector中间插入元素,会慢上许多(因为,在中间插入元素,会影响到插入位置后面所有元素的位置,需要为他们重新分配地址)。
综上可以看出来,vector相比起array需要占用更多的内存来对存储空间进行动态管理。
二、容器特征
1.顺序容器
vector是一个顺序容器,支持下标操作符,同样支持使用迭代器。
2.动态数组
vector和array在很多方面都是相似的,所以可以成vector看成一个大小可变的动态数组。
三、模板参数
1.T
元素的类型,vector::value_type
2.Alloc
allocator的类型,using to define the storage allocation model. vector::allocator_type
四、类型
member type | definition | notes |
---|---|---|
value_type | The first template parameter (T) | |
allocator_type | The second template parameter (Alloc) | defaults to: allocator<value_type> |
reference | value_type& | |
const_reference | const value_type& | |
pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
const_pointer | allocator_traits<allocator_type>::const_pointer | for the default allocator: const value_type* |
iterator | a random access iterator to value_type | convertible to const_iterator |
const_iterator | a random access iterator to const value_type | |
reverse_iterator | reverse_iterator<iterator> | |
const_reverse_iterator | reverse_iterator<const_iterator> | |
difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type | usually the same as ptrdiff_t |
size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
五、相关函数
1.构造/析构函数
1.构造函数 //constructor
2.析构函数 //destructor
3.赋值操作符 //operator = ;
2.迭代器
vector<int> vct;
vct.begin(); //begin 返回一个迭代器,指向vector的第一个元素
vct.end(); //end 返回一个迭代器,指向vector末尾元素的后一个位置
vct.rbegin(); //rbegin 返回一个反向迭代器,指向vector“反向”起始位置,也就是最后一个元素
vct.rend(); //rend 但会一个反向迭代,指向vector“反向”末尾位置的后一个元素。如果需要反向遍历访问第一个元素,则需要 -1
vct.cbegin(); //返回一个const_iterator,指向vector的第一个元素
vct.cend();
vct.crbegin(); //返回一个const_reverse_iterator,指向vector“反向”起始位置
vct.crend();
3.容器
size //返回size
max_size //返回max_size
resize //重置size的大小
capacity //查看allocator给 vector 分配容量的大小
empty //判断容器是否为空
reserve //重置vector的capacity大小
shrink_to_fit //要求vector改变其capacity去适应其size
4.获取元素
operator [] //支持下标操作符访问 同样也支持迭代器访问
at(n) //访问下表为n的那个元素
front //访问首个元素
end //访问末尾元素
5.修改
assign //指定vector的内容
push_back //从尾部插入元素
pop_back //从尾部弹出元素
insert //从中间插入元素 (效率不高)
erase //从中间删除元素 (效率不高)
swap //交换数组内容
clear //清除vector的内容
emplace //构建并且插入元素
emplace)back //在尾部构建并且插入元素
六、实例
#include <iostream>
#include <vector>
using namespace std;
void print(vector<int> vct)
{
for (auto& r : vct)
cout << r << ends;
cout << endl;
}
void main()
{
vector<int> vct_1;
vector<int> vct_2 = { 1,2,3,4,5,6,7,8,9 };
vector<int> vct_3(vct_2);
vector<int> vct_4(vct_2.rbegin(), vct_2.rend());
vct_1 = vct_2; //operator =
cout << "vector 1;" << endl;
print(vct_1);
cout << "vector 2 " << endl;
print(vct_2);
cout << "vector 3 " << endl;
print(vct_3);
cout << "vector 4 " << endl;
print(vct_4);
cout << endl << "-----------------------------" << endl;
cout << "begin : " << *vct_2.begin() << endl; // begin
cout << "end : " << *(vct_2.end() - 1) << endl; //end
cout << "rbegin : " << *(vct_2.rbegin()) << endl;
cout << "rend : " << *(vct_2.rend()-1) << endl; //注意rbegin 和 rend 与 begin 和 end 的区别。
cout << endl << "-----------------------------" << endl;
cout << "size : " << vct_1.size() << endl;
cout << "max_size : " << vct_1.max_size() << endl;
//通过两个的结果来看,size 和 max_size 并不相同
vct_4.resize(4); //重置大小为4 , 多余的元素删除,没有的元素补0 ,此处为多余
cout << "resize vct_4 :" << endl;
print(vct_4); //print : 9 8 7 6
vct_3.resize(20, 8); //重置大小为20,其余没有元素部分补8
cout << "resize vct_3 : " << endl;
print(vct_3); //print : 1 2 3 4 5 6 7 8 9 8 8 8 8 8 8 8 8 8 8 8
cout << "size : " << vct_4.size() << endl;
cout << "capacity : " << vct_4.capacity() << endl; //返回 allocator 为vector 所分配的容量的大小
cout << "max_size : " << vct_4.max_size() << endl;
vector<int> vct_5;
if (vct_5.empty()) { cout << "vct_5 is empty!" << endl; } //empty 判断容器大小是否为空
cout << "vct_4 capacity : " << vct_4.capacity() << endl; //output : 9
vct_4.reserve(100);
cout << "vct_4 capacity : " << vct_4.capacity() << endl; //output : 100
//很容易看出 reserve 函数的作用就是重置 capacity的大小
vector<int> vct_6(100);
cout << "1. capacity of vct_6: " << vct_6.capacity() << endl;
vct_6.resize(10);
cout << "2. capacity of vct_6: " << vct_6.capacity() << endl;
vct_6.shrink_to_fit(); //要求vector改变其capacity去适应其size
cout << "3. capacity of vct_6: " << vct_6.capacity() << endl;
cout << endl << "-----------------------------" << endl;
cout << "vct_1 : ";
for (int i = 0; i < vct_1.size(); i++)
{
cout << vct_1[i] << ends; //vector支持下表操作符访问
}
cout << endl;
vector <int>::iterator iter;
cout << "vct_2 : ";
for (iter = vct_2.begin(); iter != vct_2.end(); iter++)
{
cout << *iter << ends; //vector 同样也支持迭代器访问
}
cout << endl;
cout << "vct_2 中的下表为4的元素 : " << vct_2.at(4) << endl;
cout << "vct_2 中的首个元素 : " << vct_2.front() << endl;
cout << "vct_2 中的末尾元素 : " << vct_2.back() << endl;
cout << endl << "-----------------------------" << endl;
cout << "vct_1 : " ;
print(vct_1);
vct_5.assign(vct_1.begin() + 1, vct_1.end() - 1);
cout << "vct_5 : " ;
print(vct_5);
vct_5.push_back(100);
cout << "vct_5 after push_back : ";
print(vct_5);
vct_5.pop_back();
cout << "vct_5 after pop_back : ";
print(vct_5);
vct_5.insert(vct_5.begin() + 2, 100); //在其实位置的后两位插入100
cout << "vct_5 after insert : ";
print(vct_5);
vct_5.insert(vct_5.begin(), 4, 1000); //开始位置插入4个1000
cout << "vct_5 after insert : ";
print(vct_5);
vct_5.insert(vct_5.end(), vct_1.begin() + 3, vct_1.begin() + 5); //在末尾位置插入vct_1 下表为3 到下标为4 的数。这里需要注意! 是一个左闭右开区间[3,4)
cout << "vct_5 after insert : ";
print(vct_5);
vct_5.erase(vct_5.begin() + 3); //擦除某一个位置的元素
cout << "vct_5 after erase : ";
print(vct_5);
vct_5.erase(vct_5.begin(), vct_5.end() - 5); //查出某一个区间的元素
cout << "vct_5 after erase : ";
print(vct_5);
cout << "vct_2 before swap : ";
print(vct_2);
vct_2.swap(vct_5);
cout << "vct_2 after swap : ";
print(vct_2);
vct_2.emplace(vct_2.begin() + 1, 100); //在下标为1的地方插入100
cout << "vct_2 after emplace : ";
print(vct_2);
}