Next_permuation function

string s = “bca”;

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

cout << s;
what wii be answer?’
when williy come out of loop?
how this function works

Hello @dakshi,

  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.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.