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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Modifying Vectors: insert erase clear” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Modifying Vectors: insert erase clear”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- std::vector Basics: push_back size capacity
- Iterating Vectors: index range-for iterators
- Modifying Vectors: insert erase clear
- Vector vs std::array vs C Array