C++ std::vector Functions

Beginner
8 min read
Updated: Nov 2026
32 function guides
Function index

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 dynamic contiguous array.

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:

C++
#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 functions

Ask about size and spare room. reserve() avoids reallocations; shrink_to_fit() asks to free spare capacity.

FunctionDescriptionTutorial
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 functions

Read or change elements by index or at the ends. Storage is contiguous, so data() gives a T*.

FunctionDescriptionTutorial
at()Read or change one element by index, with a bounds check.Try it
front()Get the first element of the vector.Try it
back()Get the last element of the vector.Try it
data()Pointer to the contiguous element storage.Try it

Modifiers

13 functions

Add, remove, or replace elements. The back is fast; the middle shifts elements and may reallocate.

FunctionDescriptionTutorial
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 functions

Walk through the vector forwards or backwards. The c versions are read-only.

FunctionDescriptionTutorial
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 function

Access the memory helper object used by the vector.

FunctionDescriptionTutorial
get_allocator()Get a copy of the allocator the vector uses.Try it

Frequently Asked Questions

They are the functions you call on a std::vector object with a dot, such as v.push_back(1), v.reserve(100), v.at(0), or v.size(). They need #include <vector>.
This tutorial series covers 32: capacity (empty, size, max_size, capacity, reserve, shrink_to_fit), element access (at, front, back, data), modifiers (assign, assign_range, clear, insert, insert_range, emplace, erase, push_back, emplace_back, append_range, pop_back, resize, swap), iterators (begin, end, cbegin, cend, rbegin, rend, crbegin, crend), and get_allocator.
size() is how many elements you store. capacity() is how many fit in the current memory block without reallocating. reserve() raises capacity; shrink_to_fit() asks to free spare room.
A reallocation (when size would exceed capacity) invalidates all iterators, references, and pointers. insert and erase also invalidate from the change point onward. Call reserve() when you know the final size.
Read the C++ std::vector page first. Then open push_back(), reserve(), at(), and size() from the tables below.

Start with push_back()

Add an element at the end, the most common way to grow a vector.

push_back() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful