(C++17) | ||||
Sequence | ||||
(C++11) | ||||
vector | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
Member types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Non-member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Deduction guides(C++17) |
Defined in header <vector> | ||
template< class T, | (1) | |
namespace pmr{ template<class T> | (2) | (since C++17) |
std::vector
is a sequence container that encapsulates dynamic size arrays.Except for thestd::vector<bool>
partial specialization, 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.
The storage of the vector is handled automatically, being expanded 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 usingcapacity() function. Extra memory can be returned to the system via a call toshrink_to_fit()[1].
Reallocations are usually costly operations in terms of performance. Thereserve() 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:
std::vector
(forT
other thanbool) meets the requirements ofContainer,AllocatorAwareContainer(since C++11),SequenceContainer,ContiguousContainer(since C++17) andReversibleContainer.
All member functions ofstd::vector areconstexpr: it is possible to create and usestd::vector objects in the evaluation of a constant expression.However, | (since C++20) |
shrink_to_fit()
isnot available in C++98 mode.Contents |
T | - | The type of the elements.
| ||||||||||||||
Allocator | - | An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements ofAllocator.The behavior is undefined(until C++20)The program is ill-formed(since C++20) ifAllocator::value_type is not the same asT .[edit] |
The standard library provides a specialization ofstd::vector
for the typebool, which may be optimized for space efficiency.
space-efficient dynamic bitset (class template specialization)[edit] |
Operations | Invalidated |
---|---|
All read only operations | Never. |
swap,std::swap | end() |
clear,operator=,assign | Always. |
reserve,shrink_to_fit | If the vector changed capacity, all of them. If not, none. |
erase | Erased elements and all elements after them (includingend()). |
push_back,emplace_back | If the vector changed capacity, all of them. If not, onlyend(). |
insert,emplace | If the vector changed capacity, all of them. If not, only those at or after the insertion point (includingend()). |
resize | If the vector changed capacity, all of them. If not, onlyend() and any elements erased. |
pop_back | The element erased andend(). |
Member type | Definition | ||||
value_type | T [edit] | ||||
allocator_type | Allocator [edit] | ||||
size_type | Unsigned integer type (usuallystd::size_t)[edit] | ||||
difference_type | Signed integer type (usuallystd::ptrdiff_t)[edit] | ||||
reference | value_type&[edit] | ||||
const_reference | const value_type&[edit] | ||||
pointer |
| ||||
const_pointer |
| ||||
iterator |
| ||||
const_iterator |
| ||||
reverse_iterator | std::reverse_iterator<iterator>[edit] | ||||
const_reverse_iterator | std::reverse_iterator<const_iterator>[edit] |
constructs thevector (public member function)[edit] | |
destructs thevector (public member function)[edit] | |
assigns values to the container (public member function)[edit] | |
assigns values to the container (public member function)[edit] | |
(C++23) | assigns a range of values to the container (public member function)[edit] |
returns the associated allocator (public member function)[edit] | |
Element access | |
access specified element with bounds checking (public member function)[edit] | |
access specified element (public member function)[edit] | |
access the first element (public member function)[edit] | |
access the last element (public member function)[edit] | |
direct access to the underlying contiguous storage (public member function)[edit] | |
Iterators | |
(C++11) | returns an iterator to the beginning (public member function)[edit] |
(C++11) | returns an iterator to the end (public member function)[edit] |
(C++11) | returns a reverse iterator to the beginning (public member function)[edit] |
(C++11) | returns a reverse iterator to the end (public member function)[edit] |
Capacity | |
checks whether the container is empty (public member function)[edit] | |
returns the number of elements (public member function)[edit] | |
returns the maximum possible number of elements (public member function)[edit] | |
reserves storage (public member function)[edit] | |
returns the number of elements that can be held in currently allocated storage (public member function)[edit] | |
(DR*) | reduces memory usage by freeing unused memory (public member function)[edit] |
Modifiers | |
clears the contents (public member function)[edit] | |
inserts elements (public member function)[edit] | |
(C++23) | inserts a range of elements (public member function)[edit] |
(C++11) | constructs element in-place (public member function)[edit] |
erases elements (public member function)[edit] | |
adds an element to the end (public member function)[edit] | |
(C++11) | constructs an element in-place at the end (public member function)[edit] |
(C++23) | adds a range of elements to the end (public member function)[edit] |
removes the last element (public member function)[edit] | |
changes the number of elements stored (public member function)[edit] | |
swaps the contents (public member function)[edit] |
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) | lexicographically compares the values of twovector s(function template)[edit] |
specializes thestd::swap algorithm (function template)[edit] | |
erases all elements satisfying specific criteria (function template)[edit] |
Deduction guides | (since C++17) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges construction and insertion for containers |
__cpp_lib_ranges_reserve_hint | 202502L | (C++26) | ranges::approximately_sized_range,ranges::reserve_hint, and changes tostd::vector |
#include <iostream>#include <vector> int main(){// Create a vector containing integers std::vector<int> v={8,4,5,9}; // Add two more integers to vector v.push_back(6); v.push_back(9); // Overwrite element at position 2 v[2]=-1; // Print out the vectorfor(int n: v)std::cout<< n<<' ';std::cout<<'\n';}
Output:
8 4 -1 9 6 9
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 69 | C++98 | contiguity of the storage for elements ofvector was not required | required |
LWG 230 | C++98 | T was not required to beCopyConstructible(an element of type T might not be able to be constructed) | T is also required tobeCopyConstructible |
LWG 464 | C++98 | access to the underlying storage of an emptyvector resulted in UB | data function provided |
(C++26) | resizable, fixed capacity, inplace contiguous array (class template)[edit] |
(C++11) | fixed-sized inplace contiguous array (class template)[edit] |
double-ended queue (class template)[edit] |