Sort(v.begin(),v.end(),compare)

how is sort function calling compare function and why compare function when used inside sort does not have arguments

Hello @shiva57reddy,

Sort function implicitly selects the arguments for compare function at runtime in the following manner.
Thus, we do not explicitly specify it’s parameters.

Suppose you have a collection of string and you have to sort them using compare function.
For input:
6
bat
apple
dat
hello
batman
hell

Output:
apple bat
dat apple
dat bat
hello apple
hello dat
batman apple
batman hello
batman dat
batman bat
batman apple
hell apple
hell hello
apple
batman
bat
dat
hello
hell

The following picture explains the pattern/ flow of element pass.

I have numbered the comparisons,
The order of number shows the order in which comparison is taking places,
In case of true, swapping occurs and i have shown the sequence after swapping.

52

Observations:

it send the string in the right side in sequence as a and element at left side in sequence as b.

Consider comparison (7) in the figure:
In case if compare returns true, it performs swap and take the element at left after swap as current element(i.e. a=batman) and compare it the element just before it(b=dat) and continues the swap until compare returns false.

Consider comparison (2),(4),(6),(11) in the figure:
In case if compare returns false, it make the first element from left which not yet traversed(i.e., a=dat or a=hello or a=batman or a=hell) as the current element(i.e a) and then
…compares it with the first element of the sequence
…compares it with the element just before it(i.e a).

Hope, this would help to certain level.