comparator in sort c++

In C++, you can use a comparator function in the sort function to define custom sorting criteria. Here's an example of how to use a comparator in the sort function:

#include <iostream>
#include <algorithm>
#include <vector>

bool customComparator(int a, int b) {
    return a < b; // Change the comparison logic as needed
}

int main() {
    std::vector<int> vec = {3, 1, 4, 1, 5, 9};

    std::sort(vec.begin(), vec.end(), customComparator);

    for (int num : vec) {
        std::cout << num << " ";
    }

    return 0;
}

In this example, the customComparator function is used to define the sorting order. The sort function then takes the comparator function as the third argument to customize the sorting behavior.