c++STL全排列

包含在c++<algorithm>库中的next_permutation(arr,arr+n)函数可以实现arr中元素的全排列

但是要求arr中元素事先已经按字典序排列好

具体使用方法如下:

#include <iostream>
#include <algorithm>

using namespace std;

int arr[5]={1,3,2,3,4};

int main()
{
    sort(arr,arr+5);
    while(next_permutation(arr,arr+5))
    {
        for(int i=0;i<5;i++)
        {
            cout<<arr[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 

posted @ 2016-07-13 08:43  相儒以沫  阅读(1786)  评论(0编辑  收藏  举报