本文中的vector指的是std::vector C++11标准。
Vector概述
template <class T,class Alloc = allocator <T> > class vector; //通用模板
vector是表示可以改变大小的数组的序列容器。
就像数组一样,vector使用连续存储空间存储元素,这意味着它们的元素也可以使用指向其元素的指针进行偏移来访问,并与数组一样高效。但与数组不同的是, vector的大小可以动态变化,并且是由容器自动处理的。
在内部实现上,vector使用动态分配的数组来存储它们的元素。在插入新元素时,vector的大小增大,可能需要重新分配数组,这意味着可能要分配新数组并将原有数组中所有元素移动到这个新数组中。重新分配数组的时间成本相对高昂,因此,vector不会在每次向容器添加元素时都重新分配数组。vector容器可能会分配一些额外的存储空间来适应可能的增长,因此容器的实际容量可能比其包含的元素个数要大。不同库可以实现不同的增长策略以在使用现有内存和 重新分配内容之间取得平衡,但无论如何,重新分配内存时的数组大小应以对数增长,这样在vector末端插入单个元素时就可以得到平摊的常数时间复杂度。
因此,与数组相比,vector消耗更多内存,以换取以更有效的方式管理存储空间。
与其他动态序列容器(deques,lists和forward_lists)相比,vector可以非常高效地访问其元素(就像数组一样)并且相对高效地从其末尾添加或删除元素。 对于涉及在末尾以外的位置插入或删除元素的操作,性能比其他序列容器要差,并且与lists和forward_lists相比具有更少的迭代器和引用一致性。
容器属性
- 顺序存储
序列容器中的元素按照严格的线性顺序存储。各个元素通过使用它们在这个序列中的位置(index)来访问。
- 动态数组
允许直接访问序列中的任何元素,支持指针运算,相对快速的添加/删除序列末尾的元素。
- 分配器
容器使用allocator对象来动态处理其存储需求。
模板参数
- T
元素的类型。只有当 T 保证在移动操作时不会抛出异常,实现才会进行优化,即在重新分配数组时移动元素而不是复制它们。
别名为成员类型 vector :: value_type。
- Alloc
用于定义分配模型的分配器对象的类型。默认情况下,使用allocator类模板,该模板定义最简单的内存分配模型,并且与值无关。
别名为成员类型 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 |
成员函数
- (constructor) 构造函数
| 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());
|
| 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());
|
构造函数示例:
// 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 // 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; } Output: The contents of fifth are: 16 2 77 29