std::vector
| <vector> .
|
||
template< class T, class Allocator = std::allocator<T> > class vector; |
||
std::vector (sequence) .
| , . . This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array. | (since C++03) |
The storage of the vector is handled automatically, being expanded and contracted as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. The total amount of allocated memory can be queried using capacity() function. Extra memory can be returned to the system via a call to shrink_to_fit().
Reallocations are usually costly operations in terms of performance. reserve() function can be used to eliminate reallocations if the number of elements is known beforehand.
. :
- - O(1)
- / - (amortized) O(1)
- / - O(n)
std::vector meets the requirements of Container, AllocatorAwareContainer, SequenceContainer and ReversibleContainer.
Template parameters
| T | - | .
| ||||
| Allocator | - | (allocator). Allocator .
|
Specializations
bool std::vector .
| space-efficient dynamic bitset (class template specialization) |
Member types
| Member type | Definition |
value_type
|
T
|
allocator_type
|
|
size_type
|
(unsigned int) ( std::size_t) |
difference_type
|
(signed int) ( std::ptrdiff_t) |
reference
|
Allocator::reference (until C++11)value_type& (since C++11)
|
const_reference
|
Allocator::const_reference (until C++11)const value_type& (since C++11)
|
pointer
|
Allocator::pointer (until C++11)std::allocator_traits<Allocator>::pointer (since C++11)
|
const_pointer
|
Allocator::const_pointer (until C++11)std::allocator_traits<Allocator>::const_pointer (since C++11)
|
iterator
|
RandomAccessIterator
|
const_iterator
|
(constant) (iterator) |
reverse_iterator
|
std::reverse_iterator<iterator>
|
const_reverse_iterator
|
std::reverse_iterator<const_iterator>
|
Member functions
vector . (public member function) | |
vector . (public member function) | |
| . (public member function) | |
| . (public member function) | |
| . (public member function) | |
Element access | |
| access specified element with bounds checking (public member function) | |
| (public member function) | |
| . (public member function) | |
| . (public member function) | |
(C++11) |
direct access to the underlying array (public member function) |
Iterators | |
| (iterator) . (public member function) | |
| (iterator) . (public member function) | |
| (reverse iterator) . (public member function) | |
| (reverse iterator) . (public member function) | |
Capacity | |
| . (public member function) | |
| . (public member function) | |
| . (public member function) | |
| reserves storage (public member function) | |
| returns the number of elements that can be held in currently allocated storage (public member function) | |
(C++11) |
reduces memory usage by freeing unused memory (public member function) |
Modifiers | |
| . (public member function) | |
| . (public member function) | |
(C++11) |
. (public member function) |
| (public member function) | |
| . (public member function) | |
(C++11) |
constructs elements in-place at the end (public member function) |
| (public member function) | |
| (public member function) | |
| (public member function) | |
Non-member functions
| lexicographically compares the values in the vector (function template) | |
| specializes the std::swap algorithm (function template) |