Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- shadowm@nanacore:~% g++ -W -Wall -std=c++11 -o erase_test erase_test.cpp
- shadowm@nanacore:~% ./erase_test
- 0 2 4 6 8 10
- 6 7 8 9 10
- */
- #include <vector>
- #include <iostream>
- #include <algorithm>
- template<typename T>
- inline void dump(const T& set)
- {
- for(const auto& e : set) {
- std::cerr << e << ' ';
- }
- std::cerr << '\n';
- }
- int main(int, char**)
- {
- std::vector<int> foo = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
- auto bar = foo;
- auto is_odd = [](int n) { return n & 1; };
- // erase odd numbers
- foo.erase(std::remove_if(foo.begin(), foo.end(), is_odd), foo.end());
- bar.erase(bar.begin(), std::remove_if(bar.begin(), bar.end(), is_odd));
- dump(foo);
- dump(bar);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment