fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <functional>
  5.  
  6. using namespace std;
  7.  
  8. template <typename Iterator>
  9. inline bool next_combination(const Iterator first, Iterator k,
  10. const Iterator last) {
  11. if ((first == last) || (first == k) || (last == k))
  12. return false;
  13.  
  14. Iterator itr1 = first;
  15. Iterator itr2 = last;
  16. ++itr1;
  17.  
  18. if (last == itr1)
  19. return false;
  20.  
  21. itr1 = last;
  22. --itr1;
  23. itr1 = k;
  24. --itr2;
  25.  
  26. while (first != itr1) {
  27. if (*--itr1 < *itr2) {
  28. Iterator j = k;
  29.  
  30. while (!(*itr1 < *j)) ++j;
  31.  
  32. std::iter_swap(itr1, j);
  33. ++itr1;
  34. ++j;
  35. itr2 = k;
  36. std::rotate(itr1, j, last);
  37.  
  38. while (last != j) {
  39. ++j;
  40. ++itr2;
  41. }
  42.  
  43. std::rotate(k, itr2, last);
  44. return true;
  45. }
  46. }
  47.  
  48. std::rotate(first, k, last);
  49. return false;
  50. }
  51.  
  52. int main() {
  53. vector<int> combo = {1, 2, 3, 3, 3, 4, 5};
  54. sort(combo.begin(), combo.end());
  55. auto end = unique(combo.begin(), combo.end());
  56. auto begin = combo.begin();
  57. auto mid = begin + 3;
  58.  
  59. do {
  60. for (auto iter = begin; iter != mid; ++iter)
  61. cout << *iter << " ";
  62.  
  63. cout << endl;
  64. }
  65. while (next_combination(begin, mid, end));
  66. }
  67.  
Success #stdin #stdout 0.01s 5456KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5