samedi 27 janvier 2018

Understanding a Comparator functor for STL

So I have always used comparator functors in STL but never truly understood what returning a true or false meant. I had to run it and adjust the functor all the time. For instance suppose I have the following code

struct functor
{
    // does returning true place a before b ?
    bool operator()(int a,int b)
    {
        if (a < b)
            return true;
        return false;
    }
};

int main() 
{
    std::priority_queue<int, std::vector<int>,functor> q;
    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q.push(n);
}

Could anyone please clarify this

bool operator()(int a,int b)
    {
        if (a < b)
            return true;
        return false;
    }

Does returning a true place a before b?

Aucun commentaire:

Enregistrer un commentaire