std::vector (3) - Linux Manuals
std::vector: std::vector
NAME
Synopsis
Defined in header <vector>
template<
class T, (1)
class Allocator = std::allocator<T>
> class vector;
namespace pmr {
template <class T> (2) (since C++17)
using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;
}
1) std::vector is a sequence container that encapsulates dynamic size arrays.
2) std::pmr::vector is an alias template that uses a polymorphic_allocator
The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. 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().
(since C++11)
Reallocations are usually costly operations in terms of performance. The reserve() function can be used to eliminate reallocations if the number of elements is known beforehand.
The complexity (efficiency) of common operations on vectors is as follows:
* Random access - constant O(1)
* Insertion or removal of elements at the end - amortized constant O(1)
* Insertion or removal of elements - linear in the distance to the end of the vector O(n)
std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer
, ContiguousContainer
(since C++17) and ReversibleContainer.
Template parameters
T - The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since C++11)