Combination without replacement & set subtracts one after combination ends

The code below is a combination without replacement. How would you subtract one from integer 'K' and restart the combination after the first combination ends until K=1?

For example:
comb(N=items chosen from, K=set amount)

comb(N=5, K=4)
~ Output ~
0,1,2,3
0,1,2,4
0,1,3,4
0,2,3,4
1,2,3,4


comb(N=5, K=4-1)
~ Output ~
0,1,2
0,1,3
0,1,4
0,2,3
0,2,4
0,3,4
1,2,3
1,2,4
1,3,4
2,3,4


comb(N=5,K=(4-2) or (3-1))
~ Output ~
0,1
0,2
0,3
0,4
1,2
1,3
1,4
2,3
2,4
3,4


comb(5,K=(4-3) or (2-1))
~ Output ~
END


--------------------------------------------------------------------------------
Here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <algorithm>
#include <iostream>
#include <string>
 
using namespace std;

void comb(int N, int K)
{
    std::string bitmask(K, 1); // K leading 1's
    bitmask.resize(N, 0); // N-K trailing 0's
 
    // print integers and permute bitmask
    do {
        for (int i = 0; i < N; ++i) // [0..N-1] integers
        {
            if (bitmask[i]) std::cout << " " << i;
        }
        std::cout << std::endl;
    } while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
 
int main()
{
    comb(5, 4);
}


--------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <algorithm>
#include <iostream>
#include <string>
 
using namespace std;

void comb(int N, int K)
{
    std::string bitmask(K, 1); // K leading 1's
    bitmask.resize(N, 0); // N-K trailing 0's
 
    // print integers and permute bitmask
    do {
        for (int i = 0; i < N; ++i) // [0..N-1] integers
        {
            if (bitmask[i]) std::cout << " " << i;
        }
        std::cout << std::endl;
    } while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
 
int main()
{
    for(int i = 4; i > 0; i--){
          comb(5, i);
      } 
}


Thanks for all your help.
Topic archived. No new replies allowed.