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:

hello world !
hello ! world
world ! hello
world hello !
! hello world
! world hello 

However,with the code below ,i got the result like this:

helloworld!
world!hello
worldhello!

what's the problem?

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?

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.

1 Like

:b:That's good!