CKTIS / C++
std::vector — dynamic array
std::vector manages a resizable sequence of elements. Learn the difference between size and capacity, how to add and remove elements, how iterators provide access, and when pointers and references become invalid.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <vector>
std::vector<T> values;
// PMR variant:
#include <memory_resource>
std::pmr::vector<T> pooled;std::vector<T> from <vector> manages memory automatically and stores elements contiguously, except for the std::vector<bool> specialization. Indexed access costs O(1), appending costs amortized O(1), and insertion in the middle costs O(n). Entry scope: C++23; a repository compiler flag does not attest the production runtime. The source is an evolving draft, not a frozen C++23 standard.
std::vector::size reports the number of elements; std::vector::empty tests whether it is zero. std::vector::reserve(n) ensures capacity of at least n without changing size or creating elements. Reserved storage alone must not be indexed. std::vector::resize(n) changes the element count: it removes trailing elements or adds default-inserted ones; resize(n, value) adds copies of value. std::vector::clear destroys all elements while retaining capacity.
std::vector::push_back appends a copy or moves a value; std::vector::emplace_back constructs an element at the end from arguments. std::vector::insert inserts before the supplied iterator. std::vector::pop_back removes the last element without returning it. std::vector::front and std::vector::back return references to the first and last elements; these last three operations require a nonempty container. std::vector::begin and std::vector::end delimit [begin(), end()); end() must not be dereferenced. std::vector::data exposes a pointer to contiguous element storage without transferring ownership; it must not be dereferenced for an empty container.
Reallocation invalidates every pointer, reference, and iterator, including the old end(). Without reallocation, insertion invalidates them at or after the insertion point, including the old end(). std::vector::swap exchanges contents and capacity in O(1); when allocators are not swapped, they must compare equal. std::pmr::vector<T> is an alias using std::pmr::polymorphic_allocator<T>; include <memory_resource> to use a memory resource, and keep a supplied resource alive longer than the container using it.
Examples
#include <iostream>
#include <vector>
int main() {
std::vector<int> values;
values.reserve(6);
values.push_back(10);
values.emplace_back(30);
values.insert(values.begin() + 1, 20);
values.resize(4, 40);
values.pop_back();
for (auto it = values.begin(); it != values.end(); ++it) {
std::cout << *it << ' ';
}
std::cout << '\n';
std::cout << values.size() << ' ' << values.front() << ' '
<< values.back() << ' ' << values.data()[1] << '\n';
std::vector<int> other;
values.swap(other);
other.clear();
std::cout << std::boolalpha << values.empty() << ' '
<< other.empty() << '\n';
}
Result
10 20 30 3 10 30 20 true true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.