samedi 9 juin 2018

std::sort Compare's Struct Operator() Convention

On every website that uses std::sort, if the Compare function is defined, it is defined as a struct with an operator() in it. On the contrary, my code with pure function is smaller and faster. I want to know about the reasoning behind the conventional way.

My code:

static bool cmp(const int a, const int b) {
    ...
}
std::sort(a.begin(), a.end(), cmp);

The traditional way:

struct cmp {
    bool operator()(const int a, const int b) const {
        ...
    }
}
std::sort(a.begin(), a.end(), cmp());

Aucun commentaire:

Enregistrer un commentaire