Illustrating the generic prev_permutation algorithms : prev_permutation « STL Algorithms Helper « C++ Tutorial






#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
  vector<int> vector1(3);
  for (int i = 0; i < 3; ++i) 
     vector1[i] = i;

  // In lexicographical order the permutations of 0 1 2 are
  // 0 1 2, 0 2 1, 1 0 2, 1 2 0, 2 0 1, 2 1 0. 
  // Show that from 0 1 2 next_permutation produces 0 2 1:
  next_permutation(vector1.begin(), vector1.end());
  cout << vector1[0] << " ";
  cout << vector1[1] << " ";
  cout << vector1[2] << " ";


  cout << "\n\n\n\n\n\n";

  // Show that from 0 2 1 prev_permutation() produces 0 1 2:
  prev_permutation(vector1.begin(), vector1.end());
  cout << vector1[0] << " ";
  cout << vector1[1] << " ";
  cout << vector1[2] << " ";
  
  return 0;
}
0 2 1





0 1 2








32.14.prev_permutation
32.14.1.Illustrating the generic prev_permutation algorithms
32.14.2.Use prev_permutation to permute until descending sorted
32.14.3.Use prev_permutation to permute elements until they are sorted in descending order