類模板
<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 一樣

成員函式


迭代器:

容量:

元素訪問:

修改器:

分配器:

非成員函式過載


模板特化