next_permutation函数的使用

next_permutation函数的使用

next_permutation,中文意思是“下一个排列”,正如它的名字一样,它就是用来求下一个排列的。next_permutation()会取得[first,last)所标示之序列的下一个排列组合,如果没有下一个排列组合,便返回false;否则返回true。该函数包含在头文件"#include"中。

next_permutation(首指针,尾指针);

示例:给定一个字符串,求出这个字符串的字典序的全排列

#include <iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
    
    
    string str;
    cin>>str;
    sort(str.begin(),str.end());
    do{
    
    
        cout<<str<<endl;
    }while(next_permutation(str.begin(),str.end()));
    return 0;
}

注意:这个示例需要用sort进行排序,如若不然,只会输出部分排列,结果如下

string str="bca";
    do{
    
    
        cout<<str<<endl;
    }while(next_permutation(str.begin(),str.end()));
    

在这里插入图片描述
https://vj.nytdoj.com/contest/429667#problem/D

猜你喜欢

转载自blog.csdn.net/Supreme7/article/details/115267879