0Pricing
C++ Academy · Lesson

Modifying Vectors: insert erase clear

Insert, erase and clear elements while keeping iterator invalidation rules in mind.

Modifying Vectors: insert erase clear is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Inserting at the End

The fastest insert: push_back or emplace_back — both add to the end in amortized O(1).

Inserting at an Arbitrary Position

insert(pos, value) shifts everything after pos right. O(n) operation.

std::vector<int> v = {1, 2, 4, 5};
v.insert(v.begin() + 2, 3);   // {1, 2, 3, 4, 5}

Inserting a Range

Insert all elements from another container at once.

std::vector<int> v = {1, 5};
std::vector<int> mid = {2, 3, 4};
v.insert(v.begin() + 1, mid.begin(), mid.end());
// {1, 2, 3, 4, 5}

Inserting Multiple Copies

Insert n copies of the same value.

std::vector<int> v = {1, 4};
v.insert(v.begin() + 1, 2, 99);   // {1, 99, 99, 4}

Erasing One Element

erase(pos) removes the element at the iterator and shifts everything after it left. Returns an iterator to the next element.

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

Erasing a Range

Remove all elements in the half-open range [first, last).

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

The Erase-Remove Idiom

To remove all elements matching a predicate without leaving gaps, combine std::remove_if and erase.

#include <algorithm>
v.erase(
    std::remove_if(v.begin(), v.end(), [](int x) { return x < 0; }),
    v.end()
);

C++20 std::erase and std::erase_if

C++20 added free functions that wrap the erase-remove idiom.

std::erase(v, 0);                    // remove all 0s
std::erase_if(v, [](int x) { return x < 0; });

clear: Remove All Elements

clear() removes all elements but keeps the capacity. Useful when you plan to refill the vector.

pop_back: Remove the Last

O(1) removal of the last element. No return value — get it first if you need it.

int last = v.back();
v.pop_back();

Iterator Invalidation Rules

insert and erase invalidate every iterator from the modified position onward. Erase returns the next valid iterator so you can keep iterating.

Quick Check

What does std::remove_if by itself do to a vector?

Recap

insert and erase work at arbitrary positions in O(n). Use push_back/pop_back for O(1) at the end. Apply the erase-remove idiom (or C++20 std::erase_if) to delete matching elements.

Frequently asked questions

Is the “Modifying Vectors: insert erase clear” lesson free?

Yes — the full text of “Modifying Vectors: insert erase clear” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Modifying Vectors: insert erase clear”?

Insert, erase and clear elements while keeping iterator invalidation rules in mind. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modifying Vectors: insert erase clear” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. std::vector Basics: push_back size capacity
  2. Iterating Vectors: index range-for iterators
  3. Modifying Vectors: insert erase clear
  4. Vector vs std::array vs C Array
← Back to C++ Academy