Below example demonstrates the usage of erase-remove_if. limit
is captured by reference and can thus be modified outside the lambda:
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
std::vector<int> vec{0,1,2,3,4,5,6,7,8,9};
int size = vec.size();
for (int limit = 0; limit <= size; limit++)
{
vec.erase(std::remove_if(std::begin(vec), std::end(vec), [&limit](int i) {
return i < limit;
}), std::end(vec));
for (auto& v : vec)
std::cout << v;
std::cout << std::endl;
}
return 0;
}
Expected output:
0123456789
123456789
23456789
3456789
456789
56789
6789
789
89
9
1
solved C++: Using remove_if to filter vector on a condition