| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Gary Wessle" |
| Date: |
22 Aug 2006 09:54:18 PM |
| Object: |
combinations ( NchooseK) |
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
.
|
|
| User: "Mark P" |
|
| Title: Re: combinations ( NchooseK) |
23 Aug 2006 12:20:54 PM |
|
|
Gary Wessle wrote:
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.
.
|
|
|
|

|
Related Articles |
|
|