类模板
<vector>

std::vector

template < class T, class Alloc = allocator<T> > class vector; // generic template
Vector
Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

容器属性

序列
序列容器中的元素以严格的线性序列排序。单个元素通过其在此序列中的位置进行访问。
Dynamic array
Allows direct access to any element in the sequence, even through pointer arithmetics, and provides relatively fast addition/removal of elements at the end of the sequence.
感知分配器
容器使用分配器对象来动态处理其存储需求。

模板参数

T
元素的类型。
Only ifT is guaranteed to not throw while moving, implementations can optimize to move elements instead of copying them during reallocations.
别名为成员类型vector::value_type.
Alloc
用于定义存储分配模型的分配器对象类型。默认情况下,使用 allocator 类模板,它定义了最简单的内存分配模型并且与值无关。
别名为成员类型vector::allocator_type.

成员类型

成员类型定义说明
value_type第一个模板参数 (T)
allocator_type第二个模板参数 (Alloc)默认为allocator<value_type>
引用allocator_type::reference对于默认的 allocatorvalue_type&
const_referenceallocator_type::const_reference对于默认的 allocatorconst value_type&
指针allocator_type::pointer对于默认的 allocatorvalue_type*
const_pointerallocator_type::const_pointer对于默认的 allocatorconst value_type*
iterator一个指向value_type随机访问迭代器,可转换为const_iterator
const_iterator一个指向const value_type
reverse_iteratorreverse_iterator<iterator>
const_reverse_iteratorreverse_iterator<const_iterator>
difference_type一个有符号整数类型,与iterator_traits<iterator>::difference_type相同,通常与 ptrdiff_t 一样
size_type一个可以表示任何非负值的difference_type的无符号整数类型,通常与 size_t 一样
成员类型定义说明
value_type第一个模板参数 (T)
allocator_type第二个模板参数 (Alloc)默认为allocator<value_type>
引用value_type&
const_referenceconst value_type&
指针allocator_traits<allocator_type>::pointer对于默认的 allocatorvalue_type*
const_pointerallocator_traits<allocator_type>::const_pointer对于默认的 allocatorconst value_type*
iterator一个指向value_type随机访问迭代器,可转换为const_iterator
const_iterator一个指向const value_type
reverse_iteratorreverse_iterator<iterator>
const_reverse_iteratorreverse_iterator<const_iterator>
difference_type一个有符号整数类型,与
iterator_traits<iterator>::difference_type
相同,通常与 ptrdiff_t 一样
size_type一个可以表示任何非负值的difference_type的无符号整数类型,通常与 size_t 一样

成员函数


迭代器:

容量:

元素访问:

修改器:

分配器:

非成员函数重载


模板特化