algorithm - What's the easiest way to generate a list of combinations in C++? -
oftentimes, have problem property can either true or false, property b either true or false, , on. want test every combination of being true while b being false, , on. example might need following list:
[true,true,true] [true,true,false] [true,false,true] [true,false,false] [false,true,true] [false,true,false] [false,false,true] [false,false,false]
in haskell or python, can done list product function.
my question is, what's simplest and/or quickest way generate this? have done converting number binary, converting binary array. seems cumbersome because decimal binary conversion isn't trivial, , need worry padding binary leading zeroes fill array properly.
i have implemented , re-implemented kind of function in different contexts enough times wonder, there way simple enough can implement scratch when necessary -- without having think?
i'm not sure of code along these lines should work.
for( int = 0; < 8; i++ ){ printf( "[%s, %s, %s]\n", (i & 0x1)?"true":"false", ((i & 0x2) >> 1)?"true":"false" , ((i & 0x4) >> 2)?"true":"false" ); }
i'm iterating through numbers 0 7 (000 111 respectively) , isolating each bit identify boolean value.
Comments
Post a Comment