fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. template <typename typeVec1, typename typeVec2>
  6. void customPerms(typeVec1 a, typeVec2 b) {
  7.  
  8. int r = a.size(), n = b.size();
  9. int r1 = r - 1, n1 = n - 1;
  10. std::vector<int> z(r, 0);
  11. int numRows = (int) std::pow(n, r);
  12.  
  13. for (int i = 0; i < numRows; ++i) {
  14. for (int j = 0; j < r; ++j)
  15. std::cout << a[j] << b[z[j]];
  16. std::cout << std::endl;
  17.  
  18. for (int k = r1; k >= 0; --k) {
  19. if (z[k] != n1) {
  20. ++z[k];
  21. break;
  22. } else {
  23. z[k] = 0;
  24. }
  25. }
  26. }
  27. }
  28.  
  29. int main() {
  30. std::cout << "Example 1 : " << std::endl;
  31. std::vector<std::string> a1 = {"a", "b", "c"};
  32. std::vector<int> b1 = {1, 2};
  33. customPerms(a1, b1);
  34.  
  35. std::cout << "\nExample 2 : " << std::endl;
  36. std::vector<char> a2 = {'a', 'b'};
  37. std::vector<int> b2 = {1, 2, 3};
  38. customPerms(a2, b2);
  39. return 0;
  40. }
Success #stdin #stdout 0s 4260KB
stdin
Standard input is empty
stdout
Example 1 : 
a1b1c1
a1b1c2
a1b2c1
a1b2c2
a2b1c1
a2b1c2
a2b2c1
a2b2c2

Example 2 : 
a1b1
a1b2
a1b3
a2b1
a2b2
a2b3
a3b1
a3b2
a3b3