class template
<vector>
std::vector
template < class T, class Alloc = allocator<T> > class vector; // generic template
Векторы — это, так сказать, умные массивы. Они занимаются автоматическим размещением себя в памяти, расширением и сужением своего размера по мере
вставки или удаления данных. Векторы можно использовать в какой-то мере как массивы, обращаясь к элементам с помощью привычного оператора []. Случай-
ный доступ выполняется очень быстро в векторах. Также довольно быстро осуществляется добавление (или проталкивание) новых данных в конец вектора.
вставки или удаления данных. Векторы можно использовать в какой-то мере как массивы, обращаясь к элементам с помощью привычного оператора []. Случай-
ный доступ выполняется очень быстро в векторах. Также довольно быстро осуществляется добавление (или проталкивание) новых данных в конец вектора.
Когда это происходит, размер вектора автоматически увеличивается для того, чтобы было куда положить новое значение.
Container properties
- Sequence
- Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.
- 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.
- Allocator-aware
- The container uses an allocator object to dynamically handle its storage needs.
Template parameters
- T
- Type of the elements.
Only if T is guaranteed to not throw while moving, implementations can optimize to move elements instead of copying them during reallocations.
Aliased as member type vector::value_type. - Alloc
- Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type vector::allocator_type.
Member types
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 |
Member functions
- (constructor)
- Construct vector (public member function )
- (destructor)
- Vector destructor (public member function )
- operator=
- Assign content (public member function )
Iterators:
- begin
- Return iterator to beginning (public member function )
- end
- Return iterator to end (public member function )
- rbegin
- Return reverse iterator to reverse beginning (public member function )
- rend
- Return reverse iterator to reverse end (public member function )
- cbegin
- Return const_iterator to beginning (public member function )
- cend
- Return const_iterator to end (public member function )
- crbegin
- Return const_reverse_iterator to reverse beginning (public member function )
- crend
- Return const_reverse_iterator to reverse end (public member function )
Capacity:
- size
- Возвращает текущее число элементов, содержащихся в контейнере (public member function )
- max_size
- Возвращает максимальный размер, до которого может расшириться контейнер. (public member function )
- resize
- Change size (public member function )
- capacity
- Return size of allocated storage capacity (public member function )
- empty
- Test whether vector is empty (public member function )
- reserve
- Request a change in capacity (public member function )
- shrink_to_fit
- Shrink to fit (public member function )
Element access:
- operator[]
- Access element (public member function )
- at
- Access element (public member function )
- front
- Access first element (public member function )
- back
- Возвращает значение последнего элемента вектора. (public member function )
- data
- Access data (public member function )
Modifiers:
- assign
- Assign vector content (public member function )
- push_back
- Вставляет значение своего аргумента в конец вектора (конец располагается там, где находится самый большой индекс). (public member function )
- pop_back
- Удаляет последний элемент вектора. (public member function )
- insert
- Вставляют данные при обращении к произвольной позиции контейнера. (public member function )
- erase
- Удаляют данные при обращении к произвольной позиции контейнера. (public member function )
- swap
- Обменивает данные одного вектора на данные другого, при этом
порядок следования элементов не изменяется. (public member function )
- clear
- Clear content (public member function )
- emplace
- Construct and insert element (public member function )
- emplace_back
- Construct and insert element at the end (public member function )
Allocator:
- get_allocator
- Get allocator (public member function )
Non-member function overloads
- relational operators
- Relational operators for vector (function template )
- swap
- Exchange contents of vectors (function template )
Template specializations
- vector<bool>
- Vector of bool (class template specialization )