C++ std::vector erase() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

Definition

In C++, std::vector::erase() removes elements from a std::vector. You can remove one element by giving its iterator, or a range by giving two iterators.

It returns an iterator to the element that comes right after the removed ones. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Pass one iterator to remove one element, or two iterators to remove a range.

C++
v.erase(v.begin() + 1);              // remove index 1
v.erase(v.begin() + 1, v.begin() + 3);  // remove indexes 1 and 2
CallWhat it does
v.erase(pos)Removes the element that pos points to.
v.erase(first, last)Removes every element from first up to, but not including, last.

To remove only the last element, pop_back() is simpler and faster. A vector has no pop_front().

Library declarations
C++
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);

Parameters

ParameterDescription
posRequired for the first form. An iterator to the element to remove. It must not be end().
first, lastRequired for the second form. The range [first, last) to remove. last is the first element that is kept.

Return Value

Return ValueDescription
IteratorAn iterator to the element after the last removed one. If the removed element was at the back, it is end().

C++ Examples

Example 1 — Erase One Element

Remove the second element by its iterator.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {10, 20, 30, 40};

    v.erase(v.begin() + 1);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. v.begin() + 1 is an iterator to the element at index 1, which is 20.
  2. erase() removes that element, and 30 and 40 shift one place towards the front.
  3. The size drops from 4 to 3, and the remaining order is 10 30 40.

Example 2 — Erase a Range

Give two iterators to remove several elements in one call.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6};

    v.erase(v.begin() + 1, v.begin() + 4);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The range starts at index 1 (value 2) and stops before index 4 (value 5).
  2. So the elements 2 3 4 are removed. The element 5 is not removed, because last is excluded.
  3. What remains is 1 from the start and 5 6 from the end.

Example 3 — Erase While Looping

Use the returned iterator to remove all even numbers safely.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6};

    for (auto it = v.begin(); it != v.end();) {
        if (*it % 2 == 0) {
            it = v.erase(it);
        } else {
            ++it;
        }
    }

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The loop has no ++it in its header, because the iterator only moves when nothing is erased.
  2. For an even number, it = d.erase(it) removes it and gives back an iterator to the next element, so no element is skipped.
  3. For an odd number the code calls ++it. At the end only the odd numbers 1 3 5 remain.

Frequently Asked Questions

It removes one element, or a range of elements, from the vector and returns an iterator to the element after the removed ones.
Use d.erase(d.begin() + i). Vector iterators are random-access, so adding i to begin() works.
Erasing in the middle invalidates the old iterators. The returned iterator is valid and points to the next element, so you can continue your loop safely.
Only near the two ends. In the middle, the elements on one side must move, so the time grows with the size of the vector.
Use the erase-remove idiom: d.erase(std::remove(d.begin(), d.end(), value), d.end()); In C++20 you can write std::erase(d, value) instead.

Next: front()

Get the first element of the vector with std::vector.

front() 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.

15 people found this page helpful