fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class My_vec {
  5. enum { ROWS = 0, COLS = 1};
  6. std::vector<int> v;
  7. int sz[2];
  8.  
  9. public:
  10. My_vec(int rows, int cols)
  11. :v(rows*cols)
  12. {
  13. sz[ROWS] = rows;
  14. sz[COLS] = cols;
  15. }
  16.  
  17. inline int* operator[](int n) {
  18. return &v[n*sz[ROWS]];
  19. }
  20. };
  21.  
  22. int main() {
  23. My_vec v {2, 2};
  24. for (auto i = 0; i < 2; ++i)
  25. for (auto j = 0; j < 2; ++j)
  26. v[i][j] = i * 2 + j;
  27.  
  28. for (auto i = 0; i < 2; ++i) {
  29. for (auto j = 0; j < 2; ++j)
  30. std::cout << v[i][j] << ' ';
  31. std::cout << '\n';
  32. }
  33. return 0;
  34. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
0 1 
2 3