fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <memory>
  5.  
  6. class MyAbstract
  7. {
  8. public:
  9.  
  10. virtual void doStuff() = 0;
  11. virtual ~MyAbstract() {}
  12.  
  13. int id;
  14. };
  15.  
  16. class MyConcrete : public MyAbstract
  17. {
  18. public:
  19. MyConcrete(int id){this->id=id;}
  20. void doStuff() { }
  21.  
  22. };
  23.  
  24. int main()
  25. {
  26. std::vector<std::unique_ptr<MyAbstract>> myList;
  27.  
  28. myList.emplace_back(std::make_unique<MyConcrete>(1));
  29. myList.emplace_back(std::make_unique<MyConcrete>(2));
  30. myList.emplace_back(std::make_unique<MyConcrete>(3));
  31.  
  32. for(auto & item : myList)
  33. {
  34. std::cout << item->id << "\n";
  35. }
  36.  
  37. //Now delete one by
  38. for (auto it = myList.begin(); it != myList.end();)
  39. {
  40. if ((*it)->id == 2)
  41. {
  42. it = myList.erase(it);
  43. break;
  44. }
  45. else
  46. ++it;
  47. }
  48.  
  49. std::cout << "After deletion:" << std::endl;
  50.  
  51. for(auto & item : myList)
  52. {
  53. std::cout << item->id << "\n";
  54. }
  55. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
1
2
3
After deletion:
1
3