Permutations
std::prev_permutation
and std::next_permutation
return the previous smaller or next bigger permutation of the newly ordered range. If a smaller or bigger permutation is not available, the algorithms return false
. Both algorithms need bidirectional iterators. Per default the predefined sorting criterion std::less
is used. If you use your sorting criterion, it has to obey the strict weak ordering. If not, the program is undefined.
Applies the previous permutation to the range:
bool
prev_permutation
(
BiIt
first
,
BiIt
last
)
bool
prev_permutation
(
BiIt
first
,
BiIt
last
,
BiPred
pre
))
Applies the next permutation to the range:
bool
next_permutation
(
BiIt
first
,
BiIt
last
)
bool
next_permutation
(
BiIt
first
,
BiIt
last
,
BiPred
pre
)
You can easily generate with both algorithms all permutations of the range.
// permutation.cpp
...
#include
<algorithm>
...
std
::
vector
<
int
>
myInts
{
1
,
2
,
3
};
do
{
for
(
auto
i
:
myInts
)
std
::
cout
<...