Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ : "reset" std::next_permutation()

Tags:

c++

algorithm

stl

Is there a way to "reset" std::next_permutation()? Let's say I want to go over the permutations of the vector several times. The only thing I was able to find is to go through next_permutation and prev_permutation alternatively.

Thanks

like image 571
nasser-sh Avatar asked Feb 13 '23 00:02

nasser-sh


1 Answers

"resetting" would be sorting the sequence, e.g. using std::sort. Note that you have to start off with a sorted sequence if you want to enumerate all permutations using next_permutation.

Also, std::next_permutation will return false once the lexicographically smallest permutation is reached again.

like image 104
Niklas B. Avatar answered Feb 16 '23 04:02

Niklas B.