combinations ( NchooseK)

G

Gary Wessle

Hi

is there a function or lib in c++ which may already do this.

given {a,b,c}, give all the combinations which contain 2 letters,

would be {a,b}, {a,c}, {b,c}

thanks
 
M

Mark P

Gary said:
Hi

is there a function or lib in c++ which may already do this.

given {a,b,c}, give all the combinations which contain 2 letters,

would be {a,b}, {a,c}, {b,c}

thanks

Not in the standard library. Though you could make use of
std::next_permutation to achieve your result. If you want n choose k,
make a mask of length n with (n-k) 0s and k 1s: {0 0 0... 1 1 1}.
Repeatedly apply next_permutation to this mask and, for each such mask,
apply it to your array of values, keeping those values that match a 1 in
the mask.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top