fork download
  1. vector<valarray<int>> generate_3d_topology(int window) {
  2. vector<valarray<int>> ans;
  3. for (int i = -window; i <= window; ++i) {
  4. for (int j = -window; j <= window; ++j) {
  5. for (int k = -window; k <= window; ++k) {
  6. ans.push_back({i, j, k});
  7. }
  8. }
  9. }
  10. return ans;
  11. }
  12.  
  13. valarray<int> get_3d_indices(const valarray<int>& base, const valarray<int>& shift) {
  14. return base + shift;
  15. }
  16.  
  17. bool indices_in_range(const valarray<int>& indices, const valarray<int>& sizes) {
  18. for (size_t i = 0; i < indices.size(); ++i) {
  19. if (indices[i] < 0 || indices[i] >= sizes[i]) {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25.  
  26. // Для тех, кто привык индексы в отдельных переменных держать
  27. tuple<int, int, int> unpack_indices(const valarray<int>& indices) {
  28. return{indices[0], indices[1], indices[2]};
  29. }
  30.  
  31. template<typename Lambda>
  32. void iterate_3d_neighbors(const valarray<int>& base, int window, const valarray<int>& sizes, Lambda lambda) {
  33. auto topology = generate_3d_topology(window);
  34. for (const auto& neighbor : topology) {
  35. auto neighbor_indices = get_3d_indices(base, neighbor);
  36. if (!indices_in_range(neighbor_indices, sizes)) {
  37. continue;
  38. }
  39. auto [x, y, z] = unpack_indices(neighbor_indices);
  40. lambda(x, y, z);
  41. }
  42. }
  43.  
  44. ...
  45.  
  46. // Пример использования:
  47. iterate_3d_neighbors(
  48. {1, 5, 7},
  49. 4,
  50. {10, 10, 10},
  51. [v](int x, int y, int z) {
  52. cout << x << ' ' << y << ' ' << z << ' ' << a[x][y][z] << endl;
  53. });
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: ‘vector’ does not name a type
 vector<valarray<int>> generate_3d_topology(int window) {
 ^~~~~~
prog.cpp:13:1: error: ‘valarray’ does not name a type
 valarray<int> get_3d_indices(const valarray<int>& base, const valarray<int>& shift) {
 ^~~~~~~~
prog.cpp:17:29: error: ‘valarray’ does not name a type
 bool indices_in_range(const valarray<int>& indices, const valarray<int>& sizes) {
                             ^~~~~~~~
prog.cpp:17:37: error: expected ‘,’ or ‘...’ before ‘<’ token
 bool indices_in_range(const valarray<int>& indices, const valarray<int>& sizes) {
                                     ^
prog.cpp: In function ‘bool indices_in_range(int)’:
prog.cpp:18:10: error: ‘size_t’ was not declared in this scope
     for (size_t i = 0; i < indices.size(); ++i) {
          ^~~~~~
prog.cpp:18:24: error: ‘i’ was not declared in this scope
     for (size_t i = 0; i < indices.size(); ++i) {
                        ^
prog.cpp:18:28: error: ‘indices’ was not declared in this scope
     for (size_t i = 0; i < indices.size(); ++i) {
                            ^~~~~~~
prog.cpp:19:45: error: ‘sizes’ was not declared in this scope
         if (indices[i] < 0 || indices[i] >= sizes[i]) {
                                             ^~~~~
prog.cpp: At global scope:
prog.cpp:27:1: error: ‘tuple’ does not name a type
 tuple<int, int, int> unpack_indices(const valarray<int>& indices) {
 ^~~~~
prog.cpp:32:33: error: ‘valarray’ does not name a type
 void iterate_3d_neighbors(const valarray<int>& base, int window, const valarray<int>& sizes, Lambda lambda) {
                                 ^~~~~~~~
prog.cpp:32:41: error: expected ‘,’ or ‘...’ before ‘<’ token
 void iterate_3d_neighbors(const valarray<int>& base, int window, const valarray<int>& sizes, Lambda lambda) {
                                         ^
prog.cpp: In function ‘void iterate_3d_neighbors(int)’:
prog.cpp:33:42: error: ‘window’ was not declared in this scope
     auto topology = generate_3d_topology(window);
                                          ^~~~~~
prog.cpp:33:48: error: there are no arguments to ‘generate_3d_topology’ that depend on a template parameter, so a declaration of ‘generate_3d_topology’ must be available [-fpermissive]
     auto topology = generate_3d_topology(window);
                                                ^
prog.cpp:33:48: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp:35:48: error: ‘base’ was not declared in this scope
         auto neighbor_indices = get_3d_indices(base, neighbor);
                                                ^~~~
prog.cpp:36:49: error: ‘sizes’ was not declared in this scope
         if (!indices_in_range(neighbor_indices, sizes)) {
                                                 ^~~~~
prog.cpp:39:14: error: expected unqualified-id before ‘[’ token
         auto [x, y, z] = unpack_indices(neighbor_indices);
              ^
prog.cpp:40:16: error: ‘x’ was not declared in this scope
         lambda(x, y, z);
                ^
prog.cpp:40:19: error: ‘y’ was not declared in this scope
         lambda(x, y, z);
                   ^
prog.cpp:40:22: error: ‘z’ was not declared in this scope
         lambda(x, y, z);
                      ^
prog.cpp:40:23: error: there are no arguments to ‘lambda’ that depend on a template parameter, so a declaration of ‘lambda’ must be available [-fpermissive]
         lambda(x, y, z);
                       ^
prog.cpp: At global scope:
prog.cpp:44:1: error: expected unqualified-id before ‘...’ token
 ...
 ^~~
prog.cpp:48:18: error: expected unqualified-id before ‘,’ token
         {1, 5, 7},
                  ^
prog.cpp:49:9: error: expected unqualified-id before numeric constant
         4,
         ^
prog.cpp:50:21: error: expected unqualified-id before ‘,’ token
         {10, 10, 10},
                     ^
prog.cpp:51:9: error: expected unqualified-id before ‘[’ token
         [v](int x, int y, int z) {
         ^
prog.cpp:53:10: error: expected unqualified-id before ‘)’ token
         });
          ^
stdout
Standard output is empty