Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,188 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,119 people online right now. Registration is fast and FREE... Join Now!




next_permutation();

 
Reply to this topicStart new topic

next_permutation();

Pontus
1 Aug, 2007 - 01:30 AM
Post #1

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
I would like to use the function
CODE
next_permutation()
from the algorithm library.
I know how to use it on char arrays but how do i use it on strings?
Any help is appreciated
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Next_permutation();
1 Aug, 2007 - 02:36 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 523



Thanked: 3 times
Dream Kudos: 125
My Contributions
what algorithm library are you using?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Next_permutation();
1 Aug, 2007 - 04:03 AM
Post #3

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
#include <algorithm>
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Next_permutation();
1 Aug, 2007 - 04:20 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Is your goal to generate all permutations of characters within a given string, or all permutations of strings within a given set of strings? Can you provide a quick example of the input you would be giving, along with the expected output?
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Next_permutation();
1 Aug, 2007 - 04:22 AM
Post #5

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 523



Thanked: 3 times
Dream Kudos: 125
My Contributions
nice.
You are not being very eager to give information on the subject, thus making it impossible to say anything useful on the subject.

What development environment are you using?
Is that library a part of the standard libraries that came with it?

User is offlineProfile CardPM
+Quote Post

Pontus
RE: Next_permutation();
1 Aug, 2007 - 04:22 AM
Post #6

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
Ok like this:
input:
CODE

next_permutation("cat");

output
CODE

cat
cta
tca
tac
act
atc

And the library: <algorythm> is part of the stl standar library. And i'm using c++

This post has been edited by manhaeve5: 1 Aug, 2007 - 04:23 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Next_permutation();
1 Aug, 2007 - 04:24 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
That library is part of the STL, which contains standard C++ programming tools.

http://en.wikipedia.org/wiki/Standard_Template_Library

It is a fairly common function, but the user needs to provide additional information as to his end goal.

QUOTE(manhaeve5 @ 1 Aug, 2007 - 08:22 AM) *

Ok like this:
input:
CODE

next_permutation("cat");

output
CODE

cat
cta
tca
tac
act
atc

And the library: <algorythm> is part of the stl standar library. And i'm using c++

Well, you are only passing it one string...you can easily convert that string to a char array by using the .c_str() method of the string class.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Next_permutation();
1 Aug, 2007 - 04:25 AM
Post #8

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
thx, so it only works with arrays?
User is offlineProfile CardPM
+Quote Post

Xing
RE: Next_permutation();
1 Aug, 2007 - 04:26 AM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(manhaeve5 @ 1 Aug, 2007 - 03:00 PM) *

I would like to use the function
CODE
next_permutation()
from the algorithm library.
I know how to use it on char arrays but how do i use it on strings?
Any help is appreciated

See the sample code
CODE

#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>

using namespace std;

int main()
{
  string A[] = {"Dream", "In", "Code"};
  const int N = sizeof(A) / sizeof(int);
  while (next_permutation(A, A+N)) {}
  copy(A, A+N, ostream_iterator<string>(cout, "\n"));
}

User is offlineProfile CardPM
+Quote Post

Pontus
RE: Next_permutation();
1 Aug, 2007 - 04:27 AM
Post #10

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 529



Thanked: 2 times
Dream Kudos: 275
My Contributions
thank you for all your replys, i understand now
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Next_permutation();
1 Aug, 2007 - 04:29 AM
Post #11

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
No, you can make it work with strings if you're using a vector, but you don't actually want all permutations of a series of strings, you want all permutations of characters within one string. Or, you can jury rig something smile.gif :

http://marknelson.us/2002/03/01/next-permutation

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:19AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month