fork(1) download
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template <typename InputIt, typename OutputIt>
  10. bool next_combination(InputIt inFirst, InputIt inLast, OutputIt outFirst, OutputIt outLast) {
  11. assert(distance(inFirst, inLast) >= distance(outFirst, outLast));
  12.  
  13. const auto front = make_reverse_iterator(outFirst);
  14. const auto back = make_reverse_iterator(outLast);
  15. auto it = mismatch(back, front, make_reverse_iterator(inLast)).first;
  16.  
  17. const auto result = it != front;
  18.  
  19. if (result) {
  20. auto ub = upper_bound(inFirst, inLast, *it);
  21.  
  22. copy(ub, next(ub, distance(back, it) + 1), next(it).base());
  23. }
  24. return result;
  25. }
  26.  
  27. int main() {
  28. const vector<unsigned int> row{ 40, 40, 40, 50, 50, 60, 100 };
  29. vector<unsigned int> it{ row.cbegin(), next(row.cbegin(), 3) };
  30.  
  31. do {
  32. copy(it.cbegin(), it.cend(), ostream_iterator<unsigned int>(cout, " "));
  33. cout << endl;
  34. } while(next_combination(row.cbegin(), row.cend(), it.begin(), it.end()));
  35. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
40 40 40 
40 40 50 
40 40 60 
40 40 100 
40 50 50 
40 50 60 
40 50 100 
40 60 100 
50 50 60 
50 50 100 
50 60 100