c++ - Next_permutation and efficiency -
c++ - Next_permutation and efficiency -
is more efficient begin perchance random ordering of objects in range, using next_permutation step through greater permutations, followed stepping down, origin 1 time again original ordering, using prev_permutation reach last.
or, sort range before permuting, utilize next_permutation step through of them?
next_permutation
step through all permutations, not through greater permutations. no need revert , utilize prev_permutation
, , no need sort.
you need take care of fact next_permutation
homecoming false
1 time “rolls over” lexicographically lowest permutation need maintain track of number of current permutation know when stop.
that is, next iterate through possible permutations of range, no matter how starting range looks like.
size_t const num_permutations = multinomial_coefficient(range); (size_t = 0; < num_permutations; ++i) { next_permutation(range.begin(), range.end()); // utilize permutation. }
where multinomial_coefficient
multinomial coefficient of number of distinct elements in range. in simple case elements distinct, equivalent n!, factorial of number of elements.
c++ stl
Comments
Post a Comment