fork download
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. template < class BidirectionalIterator >
  10. bool next_combination(BidirectionalIterator first1,
  11. BidirectionalIterator last1,
  12. BidirectionalIterator first2,
  13. BidirectionalIterator last2)
  14. {
  15. if ((first1 == last1) || (first2 == last2)) {
  16. return false;
  17. }
  18. BidirectionalIterator m1 = last1;
  19. BidirectionalIterator m2 = last2; --m2;
  20. while (--m1 != first1 && !(*m1 < *m2)) {
  21. }
  22. bool result = (m1 == first1) && !(*first1 < *m2);
  23. if (!result) {
  24. while (first2 != m2 && !(*m1 < *first2)) {
  25. ++first2;
  26. }
  27. first1 = m1;
  28. std::iter_swap(first1, first2);
  29. ++first1;
  30. ++first2;
  31. }
  32. if ((first1 != last1) && (first2 != last2)) {
  33. m1 = last1; m2 = first2;
  34. while ((m1 != first1) && (m2 != last2)) {
  35. std::iter_swap(--m1, m2);
  36. ++m2;
  37. }
  38. std::reverse(first1, m1);
  39. std::reverse(first1, last1);
  40. std::reverse(m2, last2);
  41. std::reverse(first2, last2);
  42. }
  43. return !result;
  44. }
  45.  
  46. template < class BidirectionalIterator >
  47. bool next_combination(BidirectionalIterator first,
  48. BidirectionalIterator middle,
  49. BidirectionalIterator last)
  50. {
  51. return next_combination(first, middle, middle, last);
  52. }
  53.  
  54. int main() {
  55. vector<unsigned int> row{ 40, 40, 40, 50, 50, 60, 100 };
  56. vector<unsigned int>::iterator it = next(row.begin(), 3);
  57.  
  58. do {
  59. copy(row.begin(), it, ostream_iterator<unsigned int>(cout, " "));
  60. cout << endl;
  61. } while(next_combination(row.begin(), it, row.end()));
  62. }
Success #stdin #stdout 0s 3456KB
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