排列算法,生成排列C++permutation

排列算法生成序列的字典序排序,通过算法重排序列来生成字典序的下一个或上一个序列,返回一个bool值来指出是否还有下一个或上一个排列。

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort, std::reverse

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);
//   std::reverse (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

//  while ( std::prev_permutation(myints,myints+3) )
  while ( std::next_permutation(myints,myints+3) )
  std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] <<'\n';

  return 0;
}





版权声明:本文为blank__box原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。