-2
#include <iostream>
#include <algorithm>
using namespace std;
// define compare function to compare between char i and j
 _____________________{       // Line-1: Write proper function header

      _____________________;   // Line-2: Write correct function body

 }
int main() {
    char data[5];

    for (int i = 0; i < 5; i++)
        cin >> data[i];

    sort(data, data + 5, compare);

    for (int i = 0; i < 5; i++)
        cout << data[i] << " ";

    return 0;
}

In the above code what will be in LINE1 and LINE2 ,

sort(data, data + 5, compare);

in this why its taking the compare function, and I checked or compare function in c++ it takes two strings as arguments but here its not passing any arguments

1 Answers1

3

The sort function gathers it's objects from the range that you pass, i.e. the first two parameters.

For example, it could take data[0] and data[1] as two objects.

The next step would be to compare the two items. Usually, sorting requires comparing items.

The sort function calls compare with data[0] and data[1] to determine the ordering.

In summary, the sort function may call the comparison function to determine the ordering of the objects within the range you specified.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154