STL算法排列

来源:互联网 发布:认识linux服务器 编辑:程序博客网 时间:2024/06/05 14:15


《C++ Primer》

//所有容器适用

next_permutation(b,e)       //下一个排列-----从小到大   返回值false,表示没有下一个
next_permutation(b,e,cp)
prev_permutation(b,e)       //上一个排列-----从大到小   返回值true,表示还有下一个

prev_permutation(b,e,cp)


在使用排列的时候,必须先排列,要么从打到小,要么从小到大

/**------http://blog.csdn.net/u010579068------**/  #include<iostream>  #include<cstdio>  #include<string>  #include<vector>  #include<list>  #include<deque>  #include<algorithm>  using namespace std;    /***************************************** //所有容器适用 next_permutation(b,e)       //下一个排列-----从小到大   返回值false,表示没有下一个 next_permutation(b,e,cp) prev_permutation(b,e)       //上一个排列-----从大到小   返回值true,表示还有下一个 prev_permutation(b,e,cp) *****************************************/  /**----------------------------------------------------------------------------------  ----------------------------------------------------------------------------------**/  /************************************************************************************* std::next_permutation                 所有排序容器适用                      algorithm -------------------------------------------------------------------------------------- template <class BidirectionalIterator>   bool next_permutation (BidirectionalIterator first,                          BidirectionalIterator last );  template <class BidirectionalIterator, class Compare>   bool next_permutation (BidirectionalIterator first,                          BidirectionalIterator last, Compare comp); //eg:  *************************************************************************************/  /************************************************************************************* std::prev_permutation                 所有排序容器适用                      algorithm -------------------------------------------------------------------------------------- template <class BidirectionalIterator>   bool prev_permutation (BidirectionalIterator first,                          BidirectionalIterator last );  template <class BidirectionalIterator, class Compare>   bool prev_permutation (BidirectionalIterator first,                          BidirectionalIterator last, Compare comp); //eg:  *************************************************************************************/  int myints[3];    void init()  {      myints[0]=2;      myints[1]=1;      myints[2]=3;  }    void init2()  {      myints[0]=3;      myints[1]=2;      myints[2]=3;  }      int main()  {      // next_permutation        init();        cout << "The 3! possible permutations with 3 elements:\n";        sort (myints,myints+3);//从小到大        do      {          cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;      }      while ( next_permutation (myints,myints+3) );    //    while(next_permutation(myints,myints+3))  //        cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;        init();      cout << "The 3! possible permutations with 3 elements:\n";        sort (myints,myints+3);      reverse (myints,myints+3);//从大到小        do      {          cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;      }      while ( prev_permutation (myints,myints+3) );        cout<<"--------------------------------------------------------"<<endl;        init2();      cout << "The 3! possible permutations with 3 elements:\n";      sort (myints,myints+3);      do      {          cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;      }      while ( next_permutation (myints,myints+3) );          init2();      cout << "The 3! possible permutations with 3 elements:\n";        sort (myints,myints+3);      reverse (myints,myints+3);//从大到小        do      {          cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;      }      while ( prev_permutation (myints,myints+3) );        return 0;  }  


0 0