C++ STL next_permutation

来源:互联网 发布:mac os x 10.11.6 iso 编辑:程序博客网 时间:2024/05/29 18:24

/*next_permutation()返回的是布尔类型*/


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

/*使用大数据测试的时候,发现标准C++的效率很差,换成C写效率将快一倍多*/


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int main(){    int length;    char str[100];    gets(str);    length=strlen(str);    sort(str,str+length);    puts(str);    while(next_permutation(str,str+length))    {        puts(str);    }    return 0;}

next_permutation()的函数原理如下:
template<class BidirectionalIterator>bool next_permutation(      BidirectionalIterator _First,      BidirectionalIterator _Last);template<class BidirectionalIterator, class BinaryPredicate>bool next_permutation(      BidirectionalIterator _First,      BidirectionalIterator _Last,      BinaryPredicate _Comp );