BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List

p is a RandomAccessIterator? that defines a permutation on a set of integers from 0 to N. A permutation reorders the items in a sequence. p[i] is the new location of the ith item. These two functions reorder a sequence based on the permutation specified by p. The first version modifies the sequence in place, while the second one copies into a new sequence.
 template <class RandIter, class RandIterPerm>
 void permute(RandIter first, RandIter last, RandIterPerm p);

 template <class InIter, class RandIterP, class RandIterR>
 void permute_copy(InIter first, InIter last, RandIterP p, RandIterR result);


#include <iostream>
#include <cassert>
#include <boost/graph/detail/permutation.hpp>

int main()
  char array[] = "abcde";
  int perm[] = { 1,3,2,4,0,5 };
  char permuted_array[6];
  boost::permute_copy(array, array + 6, perm, permuted_array);
  std::cout << "original array: " << array << std::endl;
  std::cout << "permuted array: " << permuted_array << std::endl;
  for (int i = 0; i < 5; ++i)
    assert(array[i] == permuted_array[perm[i]]);
  return EXIT_SUCCESS;
The output is:
original array: abcde
permuted array: eacbd


The implementation can currently be found in a detail header of the Boost Graph Library [permutation.hpp].


Boost already has a [permutation_iterator] that provides the same functionality as the above algorithms. However, I think it would be OK to have both, since the permute() function is more convenient in some situations.

BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited December 5, 2001 7:55 am (diff)
Disclaimer: This site not officially maintained by Boost Developers