Test question 10

Given the following code snippet :

string s = “bca”;

do {
cout << s << ’ ';
} while(next_permutation(s.begin(), s.end()));

cout << s;
What is the output of the given code?

please explain the answer

Hi @sushreesamapika1512
Output for this code snippet will be :
bca cab cba abc

In here next_permutation is used to rearrange the elements in the range [first, last) into the next lexicographically greater permutation. A permutation is each one of the N! possible arrangements the elements can take (where N is the number of elements in the range). Different permutations can be ordered according to how they compare lexicographically to each other. And also the function, next_permutation(a.begin(), a.end()) returns ‘true’ if the function could rearrange the object as a lexicographically greater permutation. Otherwise, the function returns ‘false’. So as you can see we get cba as second last string because there is no lexicographically greater permutation possible for cba.

heyy but last is abc.this is incoreect