STL中的全排列

转载自:https://blog.csdn.net/xiaoquantouer/article/details/53471348

头文件:

#include<algorithm>

函数原型:

bool next_permutation(iterator start, iterator end);

next_permutation函数的返回值是布尔类型

例1:

 

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    string str="abc";
    while(next_permutation(str.begin(),str.end()))
        cout<<str<<endl;
    return 0;
}


输出:

例2:

 

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    vector<int> dp;
    dp.push_back(1);
    dp.push_back(2);
    dp.push_back(3);
    while(next_permutation(dp.begin(),dp.end())){
        cout<<dp[0]<<dp[1]<<dp[2]<<endl;
    }
    return 0;
}


输出:

next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。

prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。

猜你喜欢

转载自blog.csdn.net/lijunyan5/article/details/81542502