I can push_back each permutated set into a vector or list

  • Thread starter Thread starter Vance
  • Start date Start date
  • Tags Tags
    List Set Vector
Click For Summary
The code provided aims to generate and print all permutations of the character array {'m', 'i', 's', 'o'}. The user seeks guidance on how to store each permutation in a vector or list for further use. A suggestion is made to simplify the code by using "using namespace std;" and to ensure the correct type is used in the ostream_iterator, changing it from int to char. Additionally, it is emphasized that the necessary headers, such as <algorithm> for permutations, must be included. The main focus of the discussion revolves around creating a container, specifically a vector or list, to hold the permutations. It is clarified that the user does not need to create a custom container class but can utilize existing STL containers like std::vector or std::list to store the permutations.
Vance
Messages
180
Reaction score
0
Here is the code...
PHP:
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
 
Last edited:
Computer science news on Phys.org
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...
 
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
 
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.
 
Hurkyl said:
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
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
Replies
5
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
10
Views
2K