fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class Story
  8. {
  9. int n;
  10. public:
  11. Story(int n): n(n) { cout << "story #" << n << " created" << endl; }
  12. ~Story() { cout << "story #" << n << " destroyed" << endl; }
  13. };
  14.  
  15. int main()
  16. {
  17. vector<shared_ptr<Story>> list1, list2, list3, list4;
  18.  
  19. auto story1 = make_shared<Story>(1);
  20. auto story2 = make_shared<Story>(2);
  21.  
  22. list1.push_back(story1); list1.push_back(story2);
  23. list2.push_back(story2); list1.push_back(story1);
  24. list3.push_back(story1);
  25. list4.push_back(story2);
  26.  
  27. list1.erase(remove(begin(list1), end(list1), story1), end(list1));
  28. list2.erase(remove(begin(list2), end(list2), story1), end(list2));
  29. list3.erase(remove(begin(list3), end(list3), story1), end(list3));
  30. list4.erase(remove(begin(list4), end(list4), story1), end(list4));
  31.  
  32. story1 = nullptr; // story1 -- тоже указатель на объект
  33. cout << "story1 should be destroyed here" << endl;
  34. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
story #1 created
story #2 created
story #1 destroyed
story1 should be destroyed here
story #2 destroyed