fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class House {
  8. int id;
  9. public:
  10. House(int i=0) : id{i} {}
  11. int getId() const { return id; }
  12. };
  13.  
  14. std::ostream& operator<<(std::ostream& stream, House const& house)
  15. {
  16. stream<<"House "<<house.getId();
  17. return stream;
  18. }
  19.  
  20.  
  21. int main() {
  22. // your code goes here
  23. std::vector<std::unique_ptr<House>> houses;
  24. houses.push_back(make_unique<House>());
  25. houses.push_back(make_unique<House>(1));
  26.  
  27. for (int i = 0; i < houses.size(); i++)
  28. {
  29. cout << *houses[i]<< " is the same as " << *houses[i].get() <<endl;
  30. }
  31. cout << "-"<<endl;
  32. for (auto& phouse : houses) { // phouse is a referetence to a unique_ptr
  33. /* Do stuff using *p or p->xxx */
  34. cout << *phouse <<endl;
  35. }
  36. cout << "-"<<endl;
  37. for_each(houses.begin(), houses.end(), [](auto&p){ cout<<*p<<endl; });
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
House 0 is the same as House 0
House 1 is the same as House 1
-
House 0
House 1
-
House 0
House 1