anagram(next_permutation)

Let's talk about algorithms!

Moderator: Board moderators

Post Reply
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

anagram(next_permutation)

Post by ayon »

I tried but cannot use the STL next permutation :cry: anyone please show any example to use this?
Bj
New poster
Posts: 24
Joined: Mon Oct 17, 2005 1:39 am
Location: Sweden

Post by Bj »

Code: Select all


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	vector<int> a;
	a.push_back(4);	
	a.push_back(3);
	a.push_back(9);
	a.push_back(6);

	sort(a.begin(), a.end());

	do
	{
		copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
		cout << endl;
	} while( next_permutation(a.begin(), a.end()) );

	return 0;
}

It's very important to sort the array before you use next_permutation if you want all different permutations. It's also a good idea to use do..while instead of just while otherwise you will miss the first permutation inside the loop.
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon »

Hi Bj
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
Post Reply

Return to “Algorithms”