I am new in c++ and i have some difficulties to understand bitmask. If someone could describe in the simplest way how the following code work (http://rosettacode.org/wiki/Combinations#C.2B.2B):
#include <algorithm>
#include <iostream>
#include <string>
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, 3);
}
I understand the purpose of this code and what is the output but i don't undertstand the initialization of the string bitmask std::string bitmask(K, 1)
, and what the resize(N, 0)
do.