fork(1) download
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using vector3d = std::array<double, 3>;
  6.  
  7. class field
  8. {
  9. public:
  10. // the explicit prevents std::size_t's to be implicitely cast into fields ;-)
  11. explicit field(std::size_t size)
  12. : vectors_(size)
  13. {
  14. }
  15.  
  16. vector3d & operator[] (std::size_t index) { return vectors_[index]; }
  17. vector3d operator[] (std::size_t index) const { return vectors_[index]; }
  18.  
  19. private:
  20. // this is pretty contigous
  21. std::vector<vector3d> vectors_;
  22. };
  23.  
  24. int main()
  25. {
  26. field f(2);
  27. vector3d & v = f[1];
  28. v[2] = 5;
  29.  
  30. // also: this is a copy of v, so changing v2 will not affect f[1][2]
  31. vector3d v2 = v;
  32. v2[2] = 2;
  33.  
  34. std::cout << f[1][2] << std::endl;
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
5