[C++] next_permutation

#include <iostream>     
#include <algorithm>    
using namespace std;
int main () {
  int myints[] = {1,2,3};

  sort (myints,myints+3);
  
  do {
    cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << endl;
  } while ( next_permutation(myints,myints+3) );

  cout << "After loop: " << myints[0] << ' ' << myints[1] 
        << ' ' << myints[2] << endl;

  return 0;
}

output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *