vector guide

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
}

Ways to Construct a Vector


// Empty vector
std::vector<int> v1;

// Vector with 5 default-initialized ints (0)
std::vector<int> v2(5);

// Vector with 5 elements, each initialized to 42
std::vector<int> v3(5, 42);

// Copy constructor
std::vector<int> v4 = v3;

// Move constructor
std::vector<int> v5 = std::move(v3);

// Construct from initializer list
std::vector<int> v6 = {1, 2, 3, 4, 5};

// Construct from array
int arr[] = {10, 20, 30};
std::vector<int> v7(std::begin(arr), std::end(arr));

// Construct from iterators
std::vector<int> source = {5, 6, 7};
std::vector<int> v8(source.begin(), source.end());

Filter a Vector

To create a filtered vector based on some property of the elements, use std::copy_if or C++20 std::ranges::copy_if. Here are two examples:

C++11 and later:

#include <vector>
#include <algorithm>

std::vector<int> original = {1, 2, 3, 4, 5, 6};
std::vector<int> filtered;

std::copy_if(original.begin(), original.end(), std::back_inserter(filtered),
             [](int x) { return x % 2 == 0; });  // Keep even numbers

C++20 (with ranges):

#include <vector>
#include <ranges>

std::vector<int> original = {1, 2, 3, 4, 5, 6};
std::vector<int> filtered;

std::ranges::copy_if(original, std::back_inserter(filtered),
                    [](int x) { return x % 2 == 0; });  // Keep even numbers

Join Two Vectors

To join or concatenate two vectors, you can use std::insert to append the contents of one vector to the end of another:

#include <vector>

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};

// Append v2 to v1
v1.insert(v1.end(), v2.begin(), v2.end());

After this, v1 contains {1, 2, 3, 4, 5, 6}.

If you want to create a new vector that is the result of joining two vectors:

#include <vector>

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};

std::vector<int> joined;
joined.reserve(v1.size() + v2.size());  // Optional, to improve performance

joined.insert(joined.end(), v1.begin(), v1.end());
joined.insert(joined.end(), v2.begin(), v2.end());

edit this page