next_permutation用法

全排列

next_permutation   (默認)得到下一次排列的順序

prev_permutation   (默認)得到上一次排列的順序

<函數包含在#include<algorithm>的頭文件裏

prev_permutationnext_permutation用法相同

一、對int類型數組排列(chardouble型與int相同)

int num[100];

int cmp(int a ,int b)

{

      return a < b;   //若將 > 改爲<則相當於prev_permutation

}

next_permutation(num,num+100,cmp);

next_permutation(d,d+100,cmp);

 

三、對string類型排列

string str;

int cmp(char a ,char b)

{  

      return a<b;

}

next_permutation(str.begin(),str.end(),cmp);

 

二、對結構體一級排列

struct In

{

      int x,y;

}s[100];

int cmp(In a ,In b)

{

      return a.x<b.x;

}

next_permutation(s,s+100,cmp);

 

發佈了90 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章