fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. bool increase(std::size_t n, std::vector<std::size_t>& its)
  5. {
  6. for (auto rit = its.rbegin(); rit != its.rend(); ++rit) {
  7. ++*rit;
  8. if (*rit >= n) {
  9. *rit = 0;
  10. } else {
  11. return true;
  12. }
  13. }
  14. return false;
  15. }
  16.  
  17. template <typename F, typename C>
  18. void iterate(F f, const C& v, std::size_t k)
  19. {
  20. std::vector<std::size_t> it(k, 0);
  21.  
  22. do {
  23. f(v, it);
  24. } while (increase(v.size(), it));
  25. }
  26.  
  27. void print(const std::vector<char>& v, const std::vector<std::size_t>& it)
  28. {
  29. for (const auto e : it) {
  30. std::cout << v[e] << " ";
  31. }
  32. std::cout << std::endl;
  33. }
  34.  
  35. int main()
  36. {
  37. std::vector<char> v = {'a', 'b', 'c'};
  38.  
  39. iterate(print, v, 2);
  40. }
  41.  
Success #stdin #stdout 0s 4940KB
stdin
Standard input is empty
stdout
a a 
a b 
a c 
b a 
b b 
b c 
c a 
c b 
c c