samedi 6 juillet 2019

C++ use std::greater() in priority_queue and sort

Why the documentation for both cases says the same thing, but they are declared in an opposite way, one uses greater<int> while the other uses greater<int>(). Could anyone please shed some light on it?

The documentation priority_queue cpp library says that comp

can be Comparison object to be used to order the heap. This may be a function pointer or function object

priority_queue<int, vector<int>, greater<int>> minheap;  //works
priority_queue<int, vector<int>, greater<int>() > minheap; //why fail?

The documentation cpp library sort says the same thing, that is, comp can either be a function pointer or a function object.

sort (numbers.begin(), numbers.end(), std::greater<int>()); //works
sort (numbers.begin(), numbers.end(), std::greater<int>); //fail

Aucun commentaire:

Enregistrer un commentaire