fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<memory>
  4. using namespace std;
  5. class A
  6. {
  7. int id_;
  8. public:
  9. void setID(int id){id_ = id;}
  10. int getID(){return id_;}
  11. };
  12. class B
  13. {
  14. vector<unique_ptr<A>> vec;
  15. public:
  16. void fill(int id)
  17. {
  18. std::unique_ptr<A> a(new A());
  19. a->setID(id);
  20. vec.push_back(std::move(a));
  21. }
  22. void test()
  23. {
  24. for(const auto& n : vec)
  25. {
  26. cout<<n->getID()<<" ";
  27. //do other thing as u want .. like inserting elements
  28. }
  29. }
  30.  
  31. };
  32.  
  33. int main() {
  34. B b;
  35. b.fill(23);
  36. b.fill(1);
  37. b.fill(32);
  38. b.fill(123);
  39. b.test();
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
23 1 32 123