Remove elements from a vector inside a loop in C++
This post will discuss how to remove elements from a vector while iterating inside a loop in C++.
The idea is to use iterators to iterate the vector and call the vector::erase function if the current element matches the predicate. Since calling the erase() function on the vector element invalidates the iterator, special care needs to be taken while erasing an element. We can do that in many ways:
1. Use the return value of erase() for setting the iterator to the next element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6 }; auto it = v.begin(); while (it != v.end()) { // remove odd numbers if (*it & 1) { // `erase()` invalidates the iterator, use returned iterator it = v.erase(it); } // Notice that the iterator is incremented only on the else part (why?) else { ++it; } } for (int const &i: v) { std::cout << i << ' '; } return 0; } |
Output:
2 4 6
2. Decrement the iterator after it is passed to the erase() but before erase() is executed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6 }; for (auto it = v.begin(); it != v.end(); it++) { // remove odd numbers if (*it & 1) { // Notice that the iterator is decremented after it is passed // to `erase()` but before `erase()` is executed v.erase(it--); } } for (int const &i: v) { std::cout << i << ' '; } return 0; } |
Output:
2 4 6
3. Call erase() on a duplicate of the original iterator after advancing the original iterator to the next element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6 }; auto it = v.begin(); while (it != v.end()) { auto curr = it++; if (*curr & 1) { v.erase(curr); } } for (int const &i: v) { std::cout << i << ' '; } return 0; } |
Output:
2 4 6
Another solution is to use the std::remove_if with vector::erase, as shown below. This solution is valid as std::remove_if uses the loop behind the scenes.
Notice that the std::remove_if algorithm has no knowledge of the underlying container. It does not actually remove elements from the container but move all safe elements to the front and returns an iterator pointing to where the end should be, so they can be deleted using a single call to std::erase. This technique is commonly known as the Erase-remove idiom.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6 }; auto end = std::remove_if(v.begin(), v.end(), [](int const &i) { return i & 1; // remove odd numbers }); v.erase(end, v.end()); for (int const &i: v) { std::cout << i << ' '; } return 0; } |
Output:
2 4 6
That’s all about removing elements from a vector inside a loop in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)