PDA

View Full Version : Permutation


Vance
Jul30-04, 12:39 PM
Here is the code...
int main(){
char a[]={'m','i','s','o'};
do{
std::copy(a,a+4,std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl;
}while(std::next_permutation(a,a+4));
return 1234567890;
}

Can you tell me how I can push_back each permutated set into a vector or list or anything ?
I would like to use each of them then...
Do you have any ideas or instructions ?

(Edit) I am sorry, I change int array into char one and my first question should be how to make a string from chars above, I would first change them into "miso"- a string rather than just a char-array

Thanks
Nn

dduardo
Jul30-04, 02:46 PM
First, your code looks ugly with all the std:: junk. Just put this in the beginning of your program:

using namespace std;

Second, your code isn't going to work.

Third, look here for a good example:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q157/8/69.asp&NoWebContent=1

Vance
Jul30-04, 10:17 PM
I am sorry, I forgot to change the type passed in ostream_iterator class which should be "char" instead of int.
:shy: I am still a newie in STL :shy:, as far as I know, it depends on the compilers people use to make that code compile with or without include<iterator>. Don't forget to include<algorithm> also or permutation won't agree to go the way you want...

Thanks dduardo for the link, I know my problem can be easily solve with C but I like to do it with C++/STL better...

Vance
Jul30-04, 10:21 PM
By the way, my problem is not exactly about permutation but about grabbing each of
permutated subset, holding it as an element in a vector or a list at which I am getting stuck...

Any ideas, instructions would as always appreciated...
Again Thanks a lot

Hurkyl
Jul30-04, 10:34 PM
Dduardo's link does do it using the STL...

Anyways:

holding it as an element in a vector or a list

You simply need to use/make an object that can hold a permutation, and build a container of those objects.

Vance
Jul30-04, 10:49 PM
Dduardo's link does do it using the STL...

Anyways:



You simply need to use/make an object that can hold a permutation, and build a container of those objects.
I am sorry, i didn't come there to have a look...

Can you please be more specific about building a container, i am new and have never done anything like that before?

Thanks

Hurkyl
Jul31-04, 09:17 AM
I meant just use list or vector, as in "build a vector out of those objects"; I didn't mean to suggest you should write your own container class for this problem.