#include <iostream>
#include <list>
#include <algorithm>
#include <memory>
class Hoge {
public:
std::shared_ptr<int> p;
bool tobe_deleted;
Hoge(int n) : p(std::make_shared<int>(n)) { tobe_deleted = (n % 3 == 0) ? true : false; }
};
// not used <functional>
class EraseP {
public:
bool operator()(std::shared_ptr<Hoge> p) { return p->tobe_deleted; }
};
class Show {
public:
void operator()(std::shared_ptr<Hoge> obj) { std::cout << *(obj->p) << std::endl; }
};
int main() {
std::list<std::shared_ptr<Hoge> > mylist;
try {
for (int i = 0; i < 10; i++)
mylist.push_back(std::make_shared<Hoge>(i));
for_each(mylist.begin(), mylist.end(), Show());
// mylist.remove_if(EraseP());
mylist.erase(remove_if(mylist.begin(), mylist.end(), EraseP()), mylist.end());
for_each(mylist.begin(), mylist.end(), Show());
return 0;
} catch (...) { std::cout << "exception occured, something wrong.." << std::cout; }
}
/* end */
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPG1lbW9yeT4KCmNsYXNzIEhvZ2UgewpwdWJsaWM6CiAgc3RkOjpzaGFyZWRfcHRyPGludD4gcDsKICBib29sIHRvYmVfZGVsZXRlZDsKICBIb2dlKGludCBuKSA6IHAoc3RkOjptYWtlX3NoYXJlZDxpbnQ+KG4pKSB7IHRvYmVfZGVsZXRlZCA9IChuICUgMyA9PSAwKSA/IHRydWUgOiBmYWxzZTsgfQp9OwoKLy8gbm90IHVzZWQgPGZ1bmN0aW9uYWw+CmNsYXNzIEVyYXNlUCB7CnB1YmxpYzoKICBib29sIG9wZXJhdG9yKCkoc3RkOjpzaGFyZWRfcHRyPEhvZ2U+IHApIHsgcmV0dXJuIHAtPnRvYmVfZGVsZXRlZDsgfQp9OwoKY2xhc3MgU2hvdyB7CnB1YmxpYzoKICB2b2lkIG9wZXJhdG9yKCkoc3RkOjpzaGFyZWRfcHRyPEhvZ2U+IG9iaikgeyBzdGQ6OmNvdXQgPDwgKih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