std::vector#include <vector> is a dynamic array. It holds a pointer to an array on the heap and stores its elements contiguously there. Elements can be added and removed at runtime and the vector manages its own memory. It is available from the vector header:

#include <vector>

std::vector#include <vector><int> v;

Elements are appended with push_back() and the count is given by size():

v.push_back(1);
v.push_back(2);
v.push_back(3);
v.size();  // 3

Elements are accessed with [], or at() which throws an exception when the index is out of range:

v[0];       // 1
v[0] = 10;
v.at(5);    // Throws std::out_of_range#include <stdexcept>.

Elements can be removed from the end or all at once:

v.pop_back();  // Removes the last element.
v.clear();     // Removes everything.
v.empty();     // true

Internally, a vector tracks its size and capacity. The size is the number of elements in use and the capacity is how many elements fit before the array is full:

std::vector#include <vector><int> w;
w.push_back(1);
w.size();      // 1
w.capacity();  // At least 1.

When the array is full, push_back() allocates a larger array, copies the elements across and frees the old one. The capacity usually grows by a factor of 1.5 or 2, depending on the standard library implementation. Pointers and references to elements are invalidated when this happens:

int* p = &v[0];
v.push_back(2);  // May reallocate.
*p;              // Undefined behaviour if the vector reallocated.

When the array is full, push_back() will move the elements instead of copying them if the element's move constructor is noexcept.

reserve() allocates capacity up front, which avoids repeated reallocation when the final size is known:

v.reserve(100);
v.capacity();  // At least 100.

A vector can be created with a given size, and every element is value-initialised:

std::vector#include <vector><int> zeros(5);  // {0, 0, 0, 0, 0}

Prefer std::vector#include <vector> over a C-style array allocated with new[]. The vector frees its memory automatically and avoids the pitfalls of manually managing memory.

A vector can be initialised with a list of values:

std::vector#include <vector><int> v = {1, 2, 3};

Vectors work with range-based for loops:

for (int x : v) {
  std::cout#include <iostream> << x << "\n";
}

Inplace Vector

std::inplace_vector#include <inplace_vector><T, N> is a vector with a fixed capacity of N elements, stored inside the object itself instead of on the heap. It is available from the inplace_vector header:

#include <inplace_vector>

std::inplace_vector#include <inplace_vector><int, 3> v = {1, 2};
v.push_back(3);
v.size();      // 3
v.capacity();  // 3

Pushing an element into a full inplace vector does not reallocate it, but throws std::bad_alloc#include <new>. try_push_back returns an empty std::optional#include <optional> instead:

v.push_back(4);                  // Throws std::bad_alloc#include <new>.
v.try_push_back(4).has_value();  // false

Note

Use std::inplace_vector#include <inplace_vector> when the capacity is known and small.

References