fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <algorithm>
  4. #include <memory>
  5.  
  6. class Hoge {
  7. public:
  8. std::shared_ptr<int> p;
  9. bool tobe_deleted;
  10. Hoge(int n) : p(std::make_shared<int>(n)) { tobe_deleted = (n % 3 == 0) ? true : false; }
  11. };
  12.  
  13. // not used <functional>
  14. class EraseP {
  15. public:
  16. bool operator()(std::shared_ptr<Hoge> p) { return p->tobe_deleted; }
  17. };
  18.  
  19. class Show {
  20. public:
  21. void operator()(std::shared_ptr<Hoge> obj) { std::cout << *(obj->p) << std::endl; }
  22. };
  23.  
  24. int main() {
  25. std::list<std::shared_ptr<Hoge> > mylist;
  26. try {
  27. for (int i = 0; i < 10; i++)
  28. mylist.push_back(std::make_shared<Hoge>(i));
  29.  
  30. for_each(mylist.begin(), mylist.end(), Show());
  31. // mylist.remove_if(EraseP());
  32. mylist.erase(remove_if(mylist.begin(), mylist.end(), EraseP()), mylist.end());
  33. for_each(mylist.begin(), mylist.end(), Show());
  34. return 0;
  35. } catch (...) { std::cout << "exception occured, something wrong.." << std::cout; }
  36. }
  37. /* end */
  38.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
7
8
9
1
2
4
5
7
8