3rd parameter to STL's priority_queue

Since I'm a beginner to STL,I'm having a hard time inferring what the third parameter is really all about. I read the documentation of priority_queue here
http://en.cppreference.com/w/cpp/container/priority_queue
But I'm not able to understand the following lines of code

auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);

What I'm not getting is ; - decltype(cmp)> q3(cmp)
Also kindly tell me what should be the 3rd parameter of the priority_queue,and what's that called technically?? Functors or comparators or what??
Please let me know & I'd be grateful to anyone for helping me.

Thanks
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
This is specifying what kind of priority queue is to be created, and the name of it, and the input parameter to the constructor.

To specify what kind of priority queue is to be created, three types must be specified:
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
In this case:
The first type is an int.
The second type is a vector of int.
The third type is whatever type cmp happens to be. That's what decltype(cmp) does; it effectively means "the type of cmp".

std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
The priority queue in this case is named q3.

std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
The constructor of the priority queue is being given one parameter: cmp.

In this case, cmp is a lambda function being passed to the priority queue; the priority queue will use this lambda function to decide the order of the items in the queue. It is called a lambda function. The requirement on using it here is that it must satisfy the conditions of http://en.cppreference.com/w/cpp/concept/Compare ; whatever it is must meet those conditions to be used. cmp doesn't have to be a lambda function; it could be anything that meets those conditions.

Last edited on
Thanks @Repeater ! Your explanation was helpful indeed,can you tell me what's that 3rd parameter is called technically??
It's called "the third template parameter".

http://en.cppreference.com/w/cpp/language/template_parameters
Thanks again @ Repeater :)
That was what making me confused. Somewhere it's called comparators somewhere else called it functors
A functor is basically the type of a function. A comparator is a specific function type that well, compares things.
Topic archived. No new replies allowed.