C++ next_permutation

数组的全排列。

包含在头文件<algorithm>中。

#include <iostream>
#include <algorithm>

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:********
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop:1 2 3
*/

猜你喜欢

转载自www.cnblogs.com/Shinered/p/9313733.html
今日推荐