C++ std::vector Functions

About This Index
This page is the C++ std::vector Functions index. Each row links to a full guide with syntax, parameters, return value, three examples, and FAQs.
Learn what a std::vector is on /cpp/vector. Use this page when you need a specific member function such as push_back(), reserve(), or at().
A std::vector<T> stores elements in one block, grows with push_back(), and exposes capacity() / reserve() so you can avoid reallocations. Random access with v[i], at(), and data() is fast. The 32 functions are listed below.
How to Call a Vector Function
Include <vector>, create the vector, and call members with a dot:
#include <vector>
#include <iostream>
std::vector<int> v;
v.reserve(4);
v.push_back(10);
v.push_back(20);
int x = v.at(1); // 20, checked
std::size_t n = v.size(); // 2
std::size_t c = v.capacity(); C++ std::vector Functions Table
All function guides in this C++ tutorial series (32 tutorials), grouped by purpose.
Capacity
6 functionsAsk about size and spare room. reserve() avoids reallocations; shrink_to_fit() asks to free spare capacity.
| Function | Description | Tutorial |
|---|---|---|
empty() | Check whether the vector has no elements. | Try it |
size() | Get the number of elements in the vector. | Try it |
max_size() | Get the largest number of elements a vector could ever hold. | Try it |
capacity() | How many elements fit before the next reallocation. | Try it |
reserve() | Prepare storage so later push_back calls avoid reallocations. | Try it |
shrink_to_fit() | Ask the vector to release unused memory (C++11, non-binding). | Try it |
Element access
4 functionsRead or change elements by index or at the ends. Storage is contiguous, so data() gives a T*.
Modifiers
13 functionsAdd, remove, or replace elements. The back is fast; the middle shifts elements and may reallocate.
| Function | Description | Tutorial |
|---|---|---|
assign() | Replace all elements with new ones. | Try it |
assign_range() | Replace all elements with the items of a range (C++23). | Try it |
clear() | Remove every element from the vector. | Try it |
insert() | Add one or more elements at any position. | Try it |
insert_range() | Insert a whole range at any position (C++23). | Try it |
emplace() | Build a new element in place at any position (C++11). | Try it |
erase() | Remove one element or a range of elements. | Try it |
push_back() | Add an element at the back. | Try it |
emplace_back() | Build a new element in place at the back (C++11). | Try it |
append_range() | Append every element of a range at the end (C++23). | Try it |
pop_back() | Remove the last element. | Try it |
resize() | Change the number of elements, adding or removing at the back. | Try it |
swap() | Exchange the content of two vectors. | Try it |
Iterators
8 functionsWalk through the vector forwards or backwards. The c versions are read-only.
| Function | Description | Tutorial |
|---|---|---|
begin() | Get an iterator to the first element. | Try it |
end() | Get an iterator one place after the last element. | Try it |
cbegin() | Get a read-only iterator to the first element. | Try it |
cend() | Get a read-only iterator one place after the last element. | Try it |
rbegin() | Get a reverse iterator to the last element. | Try it |
rend() | Get the reverse end marker, just before the first element. | Try it |
crbegin() | Get a read-only reverse iterator to the last element. | Try it |
crend() | Get a read-only reverse end marker, just before the first element. | Try it |
Allocator
1 functionAccess the memory helper object used by the vector.
| Function | Description | Tutorial |
|---|---|---|
get_allocator() | Get a copy of the allocator the vector uses. | Try it |
Frequently Asked Questions
Start with push_back()
Add an element at the end, the most common way to grow a vector.
12 people found this page helpful
