Skip to content

Instantly share code, notes, and snippets.

@packrat386
Created October 15, 2020 14:49
Show Gist options
  • Save packrat386/140ba879da4cf16a2c3927d95b7f6238 to your computer and use it in GitHub Desktop.
Save packrat386/140ba879da4cf16a2c3927d95b7f6238 to your computer and use it in GitHub Desktop.
How to use remove_if
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::remove_if;
using std::cout;
using std::endl;
bool smol(vector<int> vec);
int main()
{
auto myvec = vector<vector<int>>{};
myvec.push_back(vector<int> { 1, 2, 3 });
myvec.push_back(vector<int> { 4, 5 });
myvec.push_back(vector<int> { 6 });
myvec.push_back(vector<int> { 7, 8, 9 });
myvec.push_back(vector<int> { 10 });
cout << "before remove_if:" << endl;
for (auto inner : myvec) {
for (auto i : inner) {
cout << i << " ";
}
cout << endl;
}
myvec.erase(remove_if(myvec.begin(), myvec.end(), smol), myvec.end());
cout << "after remove_if:" << endl;
for (auto inner : myvec) {
for (auto i : inner) {
cout << i << " ";
}
cout << endl;
}
}
bool smol(vector<int> vec)
{
return vec.size() < 2;
}
/* OUTPUT
[fg-386] albert > g++ --std=c++11 albert.cc
[fg-386] albert > ./a.out
before remove_if:
1 2 3
4 5
6
7 8 9
10
after remove_if:
1 2 3
4 5
7 8 9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment