Use next

zhaozj2021-02-08  445

C Standard Library provides two generated aligned algorithms: next_permutation () and prev_permutation (), but does not provide a standard function that generates a number of groups.

We can easily obtain "combination" from "Arrange" because there is a close relationship between the arrangement and the combination. Take the combination of R elements from n elements, with n! / (R! * (N-r)!)! These combinations can be generated with a full alignment of multiplex {R · 1, (N-R) · 0}, please see the sample program:

#include #include #include using namespace std;

INT main () {// From n element, all combinations of R element: // g [o] BCC5 [O] VC7 [x] const INT N = 7; const Int r = 4; //// 0 <= r <= n vector p, set; p.insert (p.End (), r, 1); p.insert (p.End (), n - r, 0); for (int i = 0; i! = p.size (); i) set.push_back (i 1); int count = 0; do {for (int i = 0; i! = p.size (); i) IF (p [i]) cout << set [i] << ""; count ; cout << "/ n";} while (P.Begin (), p.End ())) COUT << "there" << count << "combinations in total.";

The above programs are compiled under G 3.2, BCC 5.5.1, VC7, but the dead cycle is running in the VC7 version. Suspected this is the BUG of VC7, so use the following procedure to verify:

#include #include #include #include

Using namespace std;

INT main () {// is compiled by VC7, will die, incredible! // bcc5 [o] g [o] vc7 [x] vector p; p.push_back (2); puSH_BACK (2); puSH_BACK (1);

DO {copy (p.begin (), p.end (), ostream_iterator (cout, ")); cout <<" / n ";} while (prev_permutation (p.begin (), p.end ()));

It is still a cycle of the VC7 version, which seems to be a bug in the PREV_PERMUTATION () in the VC7. I submitted a bug Report to http://www.dinkumware.com/, the reply obtained was: Our Documentation Points Out, for Both Prev_Permutation andNext_Permutation That No Two Elements May Have EquivalentOrdering.

P.j. Plauger

In the case of the documentation, HTTP: //www.dinkumware.com/ The document has indeed this. In MSDN, I didn't find a similar proposal, I am afraid I can only interpret the update of the MS's documentation too slow.

转载请注明原文地址:https://www.9cbs.com/read-434.html

New Post(0)