fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. class Matrix {
  6. std::vector<std::vector<std::string>> m;
  7. public:
  8. Matrix(int x = 0, int y = 0) {
  9. m.resize(x);
  10. for (int i = 0; i < x; ++i)
  11. m[i].resize(y);
  12. }
  13.  
  14. void print() const {
  15. for(size_t i = 0; i < m.size(); ++i) {
  16. for (size_t j = 0; j < m[i].size(); ++j) {
  17. std::cout << '[' << m[i][j] << ']';
  18. }
  19. std::cout << std::endl;
  20. }
  21. }
  22.  
  23. class Proxy {
  24. std::vector<std::string> &mm;
  25. public:
  26. Proxy(std::vector<std::string> &s) : mm(s) {}
  27.  
  28. std::string& operator[](int index) {
  29. return mm[index];
  30. }
  31. };
  32.  
  33. Proxy operator[](int index) {
  34. return Proxy(m[index]);
  35. }
  36. };
  37.  
  38. int main()
  39. {
  40. Matrix m(5, 5);
  41. m.print();
  42. std::cout << std::endl;
  43.  
  44. m[2][2] = "It Works";
  45. std::cout << m[2][2] << std::endl;
  46. m.print();
  47. std::cout << std::endl;
  48.  
  49. Matrix m2(m);
  50. std::cout << m2[2][2] << std::endl;
  51. m2.print();
  52. std::cout << std::endl;
  53.  
  54. Matrix m3;
  55. m3 = m2;
  56. std::cout << m3[2][2] << std::endl;
  57. m3.print();
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5540KB
stdin
Standard input is empty
stdout
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]

It Works
[][][][][]
[][][][][]
[][][It Works][][]
[][][][][]
[][][][][]

It Works
[][][][][]
[][][][][]
[][][It Works][][]
[][][][][]
[][][][][]

It Works
[][][][][]
[][][][][]
[][][It Works][][]
[][][][][]
[][][][][]