Skip to content
C++ Better Explained
Go back
C++ Remove Element from Vector (and Remove Duplicates)
Edit page

C++ Remove Element from Vector (and Remove Duplicates)

To remove one element from a vector by position, use v.erase(v.begin() + index). To remove every element equal to a value, use the erase-remove idiom. Deleting from a vector has a few gotchas around iterators and efficiency, so let’s walk through each case clearly.


Remove by Index

vector::erase takes an iterator, not a plain number, so add the index to begin():

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {10, 20, 30, 40};
    v.erase(v.begin() + 2);   // remove the element at index 2 (the 30)

    for (int n : v) std::cout << n << " ";  // 10 20 40
    std::cout << "\n";
    return 0;
}

Everything after the removed element shifts left by one, so removing from the middle or front is O(n). Removing from the very end is cheaper with v.pop_back().


Remove by Value: The Erase-Remove Idiom

To delete every element equal to some value, the correct tool is the erase-remove idiom from <algorithm>:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {1, 2, 3, 2, 4, 2};
    v.erase(std::remove(v.begin(), v.end(), 2), v.end());

    for (int n : v) std::cout << n << " ";  // 1 3 4
    std::cout << "\n";
    return 0;
}

This looks odd at first, so here’s what happens: std::remove doesn’t actually shrink the vector. It shifts all the elements you want to keep to the front and returns an iterator to the new logical end. vector::erase then chops off the leftover tail. Doing it in this two-part way removes all matches in a single efficient pass.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Remove Elements Matching a Condition

To remove by a rule instead of an exact value — say, all even numbers — swap std::remove for std::remove_if with a lambda:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6};
    v.erase(std::remove_if(v.begin(), v.end(),
            [](int x){ return x % 2 == 0; }), v.end());

    for (int n : v) std::cout << n << " ";  // 1 3 5
    std::cout << "\n";
    return 0;
}

The lambda returns true for elements to remove. This is the cleanest way to filter a vector in place.


Remove Duplicates

The fastest approach, when order doesn’t matter, is sort then unique then erase:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {3, 1, 2, 3, 1, 4};
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    for (int n : v) std::cout << n << " ";  // 1 2 3 4
    std::cout << "\n";
    return 0;
}

std::unique only removes adjacent duplicates, which is why we sort first. If you need to preserve the original order, keep a std::set of values you’ve already seen and copy each new value into a result vector.


Quick Reference

GoalTool
Remove at indexv.erase(v.begin() + i)
Remove last elementv.pop_back()
Remove all equal to valueerase + std::remove
Remove by conditionerase + std::remove_if
Remove duplicatessort + unique + erase


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ Read CSV File: Parse Comma-Separated Data (with Examples)
Next Post
C++ Sort Vector of Structs: By Field with std::sort