c++中STL中的next_permutation函式基本用法
阿新 • • 發佈:2018-12-16
對於next_permutation函式是針對於排列組合問題的庫函式,它的排序方式是按照字典的方式排列的·:
如以下程式碼對於next_permutation函式的初步解釋:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; int main() { //next_permutation()函式是基於algorithm標頭檔案中的 //如果本身還有排列組合那將返回true,否則返回false int a[5]; for(int t=0;t<5;t++) { a[t]=t+1; } //如果要從小到大排的的話,要進行排序 sort(a,a+5);//排序 int s=0; do { s++; for(int t=0;t<5;t++) { cout<<" "<<a[t]; } cout<<endl; } while(next_permutation(a,a+5)); cout<<"sum = "<<s<<endl; return 0; }