wired problem about next_permutation


 
Thread Tools Search this Thread
Top Forums Programming wired problem about next_permutation
# 1  
Old 03-05-2011
wired problem about next_permutation

hello all,
If i have a vector of strings,i wanna get all possible combinations of them,for example,the elements of that vector are
"hello" "world" "!"
I thought the answer should be:
Code:
hello world !
hello ! world
world ! hello
world hello !
! hello world
! world hello

However,with the code below ,i got the result like this:
Code:
helloworld!
world!hello
worldhello!

what's the problem?
Code:
int main()
{
	string ch[] = {"hello","world","!"};
	vector<string> vec(ch,ch+3);

	do{
		vector<string>::iterator pos = vec.begin();	
		cout<<*pos<<*(pos+1)<<*(pos+2)<<endl;
	}while(next_permutation(vec.begin(),vec.end()));
	
}

---------- Post updated at 08:49 AM ---------- Previous update was at 08:02 AM ----------

Ooh.i checked the definition of next_permutation.This algorithm earranges the elements in the range [first, last) into the lexicographically next greater permutation of elements.I think it means if i want to produce all the combinations,it's better to sort the vector so that i can get the very first permutation.So,when the elements are 1,2,3 and 2,1,3,they are different,Is that right?
# 2  
Old 03-06-2011
I would suggest to have array of integers to permute. It can be initialized with A[i]=i
so no need to sort strings.
Then use permuted indexes to access strings.
This User Gave Thanks to odys For This Post:
# 3  
Old 03-06-2011
Quote:
Originally Posted by odys
I would suggest to have array of integers to permute. It can be initialized with A[i]=i
so no need to sort strings.
Then use permuted indexes to access strings.
SmilieThat's good!
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop wired output issue

Hi, Wired output I am getting. Pls let me know what mistake i did on below loop script paste profile.txt names.txt | while read i j do echo >> profiles.ini echo Name=$j >> profiles.ini echo IsRelative=1 >> profiles.ini echo Path=Profiles/$j >> profiles.ini done ... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. Red Hat

hostapd & wired network ?

hi im using the following network with hostapd on the authenticator : Authentication server <---wired---> Authenticator(hostapd<----wired---> User (win XP with WinRadius) 1.100 -------- 1.200 , 0.13 ----- 0.12 and this is my configurations for hostapd : interface=eth1 driver=wired... (0 Replies)
Discussion started by: turner
0 Replies

3. What is on Your Mind?

Wired keyboard sniffing

Are we safe using the everyday wired keyboard? Although this concept is old, I had never seen an actual implementation on the matter until a few days ago. (Four ways of sniffing the electromagnetic emanations of wired keyboards currently on the market in up to 20 meters.) Check the videos at:... (2 Replies)
Discussion started by: redoubtable
2 Replies

4. UNIX for Advanced & Expert Users

wired pages

hi, can any body tell, what are wired pages in HP_UX. which structure contains that and plz tell the corresponding system call to get it. (1 Reply)
Discussion started by: venkat_t
1 Replies

5. Programming

wired and inactive pages

Hello How to find out wired pages and inactive pages in HP -UNIX. Bye (1 Reply)
Discussion started by: manjunath
1 Replies
Login or Register to Ask a Question