fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <typename T>
  5. bool increase(const std::vector<std::vector<T>>& v, std::vector<std::size_t>& it)
  6. {
  7. for (std::size_t i = 0, size = it.size(); i != size; ++i) {
  8. const std::size_t index = size - 1 - i;
  9. ++it[index];
  10. if (it[index] == v[index].size()) {
  11. it[index] = 0;
  12. } else {
  13. return true;
  14. }
  15. }
  16. return false;
  17. }
  18.  
  19. template <typename T>
  20. void do_job(const std::vector<std::vector<T>>& v, std::vector<std::size_t>& it)
  21. {
  22. for (std::size_t i = 0, size = v.size(); i != size; ++i) {
  23. std::cout << v[i][it[i]] << " ";
  24. }
  25. std::cout << std::endl;
  26. }
  27.  
  28. template <typename T>
  29. void iterate(const std::vector<std::vector<T>>& v)
  30. {
  31. std::vector<std::size_t> it(v.size(), 0);
  32.  
  33. do {
  34. do_job(v, it);
  35. } while (increase(v, it));
  36. }
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40. std::vector<std::vector<int>> v = {
  41. {0, 1, 2, 3, 4, 5},
  42. {0, 1, 2, 3, 4, 5},
  43. {0, 1, 2, 3, 4, 5}
  44. };
  45.  
  46. iterate(v);
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
0 0 0 
0 0 1 
0 0 2 
0 0 3 
0 0 4 
0 0 5 
0 1 0 
0 1 1 
0 1 2 
0 1 3 
0 1 4 
0 1 5 
0 2 0 
0 2 1 
0 2 2 
0 2 3 
0 2 4 
0 2 5 
0 3 0 
0 3 1 
0 3 2 
0 3 3 
0 3 4 
0 3 5 
0 4 0 
0 4 1 
0 4 2 
0 4 3 
0 4 4 
0 4 5 
0 5 0 
0 5 1 
0 5 2 
0 5 3 
0 5 4 
0 5 5 
1 0 0 
1 0 1 
1 0 2 
1 0 3 
1 0 4 
1 0 5 
1 1 0 
1 1 1 
1 1 2 
1 1 3 
1 1 4 
1 1 5 
1 2 0 
1 2 1 
1 2 2 
1 2 3 
1 2 4 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