Q10 unable to understand

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?

bca cab cba abc

bca cab cba cba

bca bca bca bca

bca cab cba bca

in the output why abc the last element

Hey @bhattanurag426

  1. Output:
    Execute the following code:
    https://ide.codingblocks.com/s/224293
  2. When will it come out of the loop?
    It depends upon the terminating condition specified in the following code segment:
    while( next_permutation(s.begin(), s.end()) )
    So, when next_permutation(s.begin(), s.end()) will return false, the function will terminate.
  3. How does this function work?
    Read this link . It will provide a description of the function.

Brief:
Return value:

  • true if the function could rearrange the object as a lexicographically greater permutation.
  • Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

Explanation:
It converts the string passed as a parameter to it’s next greater permutation if exists and returns true.
if the greater permutation does not exist then to the smallest permutation and returns false.

Hope, this would help.
Give a like if you are satisfied.