fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Obj
  6. {
  7. public:
  8. Obj(int x, int y) : _x(x), _y(y)
  9. {
  10.  
  11. }
  12.  
  13. int _x;
  14. int _y;
  15. };
  16.  
  17. int main()
  18. {
  19. std::vector<Obj> obj;
  20. obj.emplace_back(10, 20);
  21. std::cout << obj[0]._x << " " << obj[0]._y << std::endl;
  22. obj[0]._x = 100;
  23. std::cout << obj[0]._x << " " << obj[0]._y << std::endl;
  24.  
  25. std::vector<Obj> obj2;
  26. obj2.reserve(1);
  27. obj2[0] = Obj(30, 40);
  28. std::cout << obj2[0]._x << " " << obj2[0]._y << std::endl;
  29. obj2[0]._x = 100;
  30. std::cout << obj2[0]._x << " " << obj2[0]._y << std::endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
10 20
100 20
30 40
100 40