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