Next_premutation
今天写到一个全排列的问题,发现题解中有dalao提到了C++的STL库中有next_premutation这个优秀的全排列函数,在CSDN上找了找了,找到许多优秀的blog,先将其汇集起来以备不时之需.
用法总结】C++ STL中 next_permutation函数的用法
C++的官方解释
具体用法
抄个C++的官方代码
// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort
int main () {
int myints[] = {1,2,3};
std::sort (myints,myints+3);
std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::next_permutation(myints,myints+3) );
std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
return 0;
/*Output
The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3*/
}