fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. struct Customer
  6. {
  7. Customer(const char* name_, int i_, int j_)
  8. : m_name(name_), m_i(i_), m_j(j_) {}
  9. ~Customer()
  10. {
  11. std::cout << "~Customer(" << m_name << ")\n";
  12. }
  13.  
  14. const char* m_name;
  15. int m_i, m_j;
  16.  
  17. const char* getName() const noexcept { return m_name; }
  18. int getPhone() const noexcept { return m_i; }
  19. int getID() const noexcept { return m_j; }
  20. };
  21.  
  22. int main()
  23. {
  24. std::vector<std::unique_ptr<Customer>> customers;
  25.  
  26. customers.emplace_back(std::make_unique<Customer>("Andy", 123, 111));
  27. customers.emplace_back(std::make_unique<Customer>("Bob", 124, 222));
  28. customers.emplace_back(std::make_unique<Customer>("Chris", 125, 333));
  29.  
  30. for (auto& ptr : customers) {
  31. std::cout << ptr->getName() << "\n";
  32. std::cout << ptr->getPhone() << "\n";
  33. std::cout << ptr->getID() << "\n";
  34. std::cout << "\n";
  35. }
  36.  
  37. // remove the first customer
  38. std::cout << "pop:\n";
  39. customers.erase(customers.begin());
  40.  
  41. // remove the rest
  42. std::cout << "clear:\n";
  43. customers.clear();
  44. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Andy
123
111

Bob
124
222

Chris
125
333

pop:
~Customer(Andy)
clear:
~Customer(Bob)
~Customer(Chris)