fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. bool increment(std::vector<std::size_t>& v, std::size_t maxSize)
  5. {
  6. for (auto it = v.rbegin(); it != v.rend(); ++it) {
  7. ++*it;
  8. if (*it != maxSize) {
  9. return true;
  10. }
  11. *it = 0;
  12. }
  13. return false;
  14. }
  15.  
  16. void print(const std::vector<int>&v, const std::vector<std::size_t>& indexes)
  17. {
  18. const char* sep = "";
  19. for (auto index : indexes) {
  20. std::cout << sep << v[index];
  21. sep = ".";
  22. }
  23. std::cout << std::endl;
  24. }
  25.  
  26. void print_cartesian_product(const std::vector<int>&v, int n)
  27. {
  28. std::vector<std::size_t> indexes(n);
  29.  
  30. do {
  31. print(v, indexes);
  32. } while (increment(indexes, v.size()));
  33.  
  34. }
  35.  
  36. int main()
  37. {
  38. print_cartesian_product({4, 8, 42}, 3);
  39. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
4.4.4
4.4.8
4.4.42
4.8.4
4.8.8
4.8.42
4.42.4
4.42.8
4.42.42
8.4.4
8.4.8
8.4.42
8.8.4
8.8.8
8.8.42
8.42.4
8.42.8
8.42.42
42.4.4
42.4.8
42.4.42
42.8.4
42.8.8
42.8.42
42.42.4
42.42.8
42.42.42