1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main()
6{
7 std::vector<int> v = { 1, 2, 3, 4, 5, 6 };
8
9 auto it = v.begin();
10 while (it != v.end())
11 {
12 // specify condition for removing element; in this case remove odd numbers
13 if (*it & 1) {
14 // erase() invalidates the iterator, use returned iterator
15 it = v.erase(it);
16 }
17 // Notice that iterator is incremented only on the else part (why?)
18 else {
19 ++it;
20 }
21 }
22
23 for (int const &i: v) {
24 std::cout << i << ' ';
25 }
26
27 return 0;
28}
29