fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Row //Proxy
  5. {
  6. public:
  7. Row(std::size_t size=0)
  8. :cols_(size){};
  9.  
  10. int& operator[](int index) { return cols_[index]; }
  11. const int& operator[](int index) const { return cols_[index]; }
  12. private:
  13. std::vector<int> cols_; // или std::array<> или просто обычный массив.
  14. };
  15.  
  16. class Matrix
  17. {
  18. public:
  19. Matrix(std::size_t rowSize, std::size_t colSize)
  20. :rows_(rowSize, Row(colSize) ){}
  21.  
  22. Row& operator[](int index) { return rows_[index]; }
  23. const Row& operator[](int index)const { return rows_[index]; }
  24.  
  25. private:
  26. std::size_t colSize_;
  27. std::vector<Row> rows_;
  28. };
  29.  
  30.  
  31. int main()
  32. {
  33. Matrix m(3,4);
  34.  
  35. m[2][2]= 5;
  36.  
  37. std::cout<< m[2][2] << std::endl;
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
5