shadowm

Untitled

Aug 19th, 2015
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /*
  2. shadowm@nanacore:~% g++ -W -Wall -std=c++11 -o erase_test erase_test.cpp
  3. shadowm@nanacore:~% ./erase_test
  4. 0 2 4 6 8 10
  5. 6 7 8 9 10
  6. */
  7.  
  8. #include <vector>
  9. #include <iostream>
  10. #include <algorithm>
  11.  
  12. template<typename T>
  13. inline void dump(const T& set)
  14. {
  15. for(const auto& e : set) {
  16. std::cerr << e << ' ';
  17. }
  18.  
  19. std::cerr << '\n';
  20. }
  21.  
  22. int main(int, char**)
  23. {
  24. std::vector<int> foo = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  25. auto bar = foo;
  26.  
  27. auto is_odd = [](int n) { return n & 1; };
  28.  
  29. // erase odd numbers
  30. foo.erase(std::remove_if(foo.begin(), foo.end(), is_odd), foo.end());
  31. bar.erase(bar.begin(), std::remove_if(bar.begin(), bar.end(), is_odd));
  32.  
  33. dump(foo);
  34. dump(bar);
  35.  
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment