Section

std::vector

std::vector is a sequence container that encapsulates a dynamically sized contiguous array.

Its elements are stored contiguously, so a pointer to an element can be passed to APIs that expect a pointer into an array. That contiguous-storage guarantee is one of the main reasons vector is the default owning sequence container in modern C++.

# Declarations

template<
class T,
class Allocator = std::allocator<T>
> class vector;
namespace pmr {
template< class T >
using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;
}

(since C++17)

# Template parameters

  • T: element type. The exact requirements depend on the operations used. In general, elements must be erasable, and many operations impose stronger construction, assignment, or move requirements.
  • Allocator: allocator used to obtain storage and manage element lifetimes. It must satisfy the Allocator requirements. Since C++20, the program is ill-formed if Allocator::value_type is not T.

# Incomplete-type support

Since C++17, vector itself may be instantiated with an incomplete element type if the allocator satisfies the allocator completeness requirements. Individual member functions may still require T to be complete at the point of use.

# Specializations

  • std::pmr::vector<T> is an alias for std::vector<T, std::pmr::polymorphic_allocator<T>>.
  • std::vector<bool> is a space-optimized partial specialization with proxy-reference behavior and different practical tradeoffs from ordinary vector<T>.

# Example

#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 vector
    for (int n : v)
        std::cout << n << ' ';
    std::cout << '\n';
}

This is the common vector usage pattern: contiguous owning storage, random-access element updates, and amortized constant-time growth at the end.

# Member types

Member typeDefinition
value_typeT
allocator_typeAllocator
size_typeUnsigned integer type, usually std::size_t
difference_typeSigned integer type, usually std::ptrdiff_t
referencevalue_type&
const_referenceconst value_type&
pointerallocator-aware pointer type to value_type
const_pointerallocator-aware pointer type to const value_type
iteratorRandom-access contiguous iterator to value_type
const_iteratorRandom-access contiguous iterator to const value_type
reverse_iteratorstd::reverse_iterator<iterator>
const_reverse_iteratorstd::reverse_iterator<const_iterator>

# Iterator invalidation

OperationInvalidation
Read-only operationsNever invalidate iterators or references
swap, std::swapend() may change
clear, assign, operator=Always invalidate all iterators, references, and pointers
reserve, shrink_to_fitIf capacity changes, all iterators, references, and pointers are invalidated; otherwise none
eraseErased elements and all elements after them, including end()
push_back, emplace_backIf capacity changes, all iterators, references, and pointers; otherwise only end()
insert, emplaceIf capacity changes, all iterators, references, and pointers; otherwise those at or after the insertion point, including end()
resizeIf capacity changes, all iterators, references, and pointers; otherwise end() and any erased elements
pop_backThe erased element and end()

Capacity growth is therefore the main boundary between stable and unstable references in vector code.

# Reference map

AreaKey entries
Construction and lifetimevector::vector, vector::~vector, vector::operator=, assign, assign_range, get_allocator
Element accessat, operator[], front, back, data
Iteratorsbegin, cbegin, end, cend, rbegin, crbegin, rend, crend
Capacityempty, size, max_size, reserve, capacity, shrink_to_fit
Modifiersclear, insert, insert_range, emplace, erase, push_back, emplace_back, append_range, pop_back, resize, swap
Non-member functionscomparison operators, std::swap(std::vector), erase, erase_if
Related surfacededuction guides, std::vector<bool>, std::pmr::vector

# Deduction guides

The class has deduction guides so direct construction can infer T and, where applicable, allocator-related types from constructor arguments.

# Notes

vector is usually the right default when you need random-access iteration, amortized constant-time append at the end, and contiguous storage.

Insertion or erasure in the middle is comparatively expensive because elements after the affected position must be moved or shifted.

# Feature-test macros

MacroValueStdFeature
__cpp_lib_incomplete_container_elements201505LC++17minimal incomplete-type support
__cpp_lib_containers_ranges202202LC++23ranges construction and insertion for containers

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 69C++98contiguity of the storage for elements of vector was not requiredrequired
LWG 230C++98T was not required to be CopyConstructibleT is also required to be CopyConstructible
LWG 464C++98access to the underlying storage of an empty vector resulted in UBdata() provided

# See also