fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Map
  5. {
  6. int width;
  7. int height;
  8. std::vector<char> field;
  9.  
  10. Map(int width, int height, char value = ' ')
  11. : width{ width }, height{ height }, field(height * width, value)
  12. {
  13. }
  14.  
  15. auto operator()(int i, int j) const -> char
  16. {
  17. return field[i * width + j];
  18. }
  19.  
  20. auto operator()(int i, int j) -> char&
  21. {
  22. return field[i * width + j];
  23. }
  24. };
  25.  
  26. auto main() -> int
  27. {
  28. Map m{ 10, 10, '.' };
  29. m(3, 3) = 'x';
  30. m(5, 0) = 'x';
  31. m(0, 5) = 'x';
  32. for (int i = 0; i < m.height; ++i)
  33. {
  34. for (int j = 0; j < m.width; ++j)
  35. std::cout << m(i, j);
  36. std::cout << "\n";
  37. }
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 4428KB
stdin
Standard input is empty
stdout
.....x....
..........
..........
...x......
..........
x.........
..........
..........
..........
..........