Guys,

I have a question regarding the use of STL next permutation algorithm

say I have a vector a[] = [ 52 3 4 2 49]

How do I use the next_permutation algorithm for only the last 3 elements (in this case 4 2 and 49) and store those in a different array?


Any help would be appreciated, please let me know if you need more information.

#include <algorithm>
#include <vector>
#include <iterator>
#include <boost/assign.hpp>
#include <cassert>
#include <iostream>
using namespace std;
using namespace boost::assign;

int main()
{
  vector<int> sequence ;
  sequence += 52, 3, 4, 2, 49 ;
  enum { N = 3 } ;
  assert( sequence.size() >= N ) ;
  vector<int> last_n( sequence.end() - N, sequence.end() ) ;
  sort( last_n.begin(), last_n.end() ) ;
  do
  {
    copy( last_n.begin(), last_n.end(), ostream_iterator<int>(cout," ") ) ;
    cout << '\n' ;
  } while( next_permutation( last_n.begin(), last_n.end() ) ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.