vector


Basics

Include Header

#include <vector>

Declaration

std::vector<type> vec_name;

Insertion


vec_name.push_back(value);                      // Add to end
vec_name.insert(vec_name.begin() + index, value);  // Insert at position

Access Element


Type val = vec_name[index];         // Direct access
Type &val = vec_name.at(index);      // Bounds-checked access

Check if Empty


if (vec_name.empty()) {
  // Vector is empty
}

Erase Element


vec_name.erase(vec_name.begin() + index);  // Remove at index
vec_name.clear();                          // Remove all elements

Iterate Through Elements


for (const auto& val : vec_name) {
  std::cout << val << std::endl;
}

In reverse order:


for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
  std::cout << *it << " ";
}

Reverse the order of a Vector


std::reverse(vec.begin(), vec.end());

Size and Capacity


vec_name.size();            // Number of elements
vec_name.capacity();        // Allocated storage capacity
vec_name.resize(new_size);  // Resize vector

Other Useful Functions

    
vec_name.front();       // First element
vec_name.back();        // Last element
vec_name.pop_back();    // Remove last element
    

Check if Element Exists

#include <algorithm>

if (std::find(vec_name.begin(), vec_name.end(), value) != vec_name.end()) {
  // Element found
} else {
  // Element not found
}

declaration

    
template<class>T, class Allocator = std::allocator<t>> class vector;
    
T is the type of the stored values
Allocator is the allocator type used to manage memory

member types

Member type Definition Explanation
value_type T The type of elements stored in the vector.
allocator_type Allocator The allocator used by the vector to manage memory.
size_type Unsigned integer type (usually std::size_t) Used for sizes and indices.
difference_type Signed integer type (usually std::ptrdiff_t) Used for differences between iterators.
reference value_type& Reference to an element, allows modification.
const_reference const value_type& Const reference to an element, read-only access.
pointer Allocator::pointer (until C++11)
std::allocator_traits<Allocator>::pointer (since C++11)
Pointer type to elements in the vector.
const_pointer Allocator::const_pointer (until C++11)
std::allocator_traits<Allocator>::const_pointer (since C++11)
Const pointer type to elements in the vector.
iterator LegacyRandomAccessIterator and LegacyContiguousIterator to value_type (until C++20)
LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to value_type (since C++20)
Iterator type that can traverse and modify elements.
const_iterator LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type (until C++20)
LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to const value_type (since C++20)
Iterator type that can traverse elements but cannot modify them.
reverse_iterator std::reverse_iterator<iterator> Iterator that traverses elements in reverse order.
const_reverse_iterator std::reverse_iterator<const_iterator> Const iterator that traverses elements in reverse order.

member functions

element access

iterators

capacity

modifiers