Paul
unread,Oct 8, 2018, 6:07:40 PM10/8/18You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to
The below code contains a definition
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
and another definition
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
I don't understand why these definitions have different forms?
I would expect either
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3;
or
std::priority_queue<int, std::vector<int>, std::greater<int> > q2(std::greater<int>);
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
As written, there doesn't seem to be consistent syntax between the two
definitions.
Thank you,
Paul
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
template<typename T> void print_queue(T& q) {
while(!q.empty()) {
std::cout << q.top() << " ";
q.pop();
}
std::cout << '\n';
}
int main() {
std::priority_queue<int> q;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q.push(n);
print_queue(q);
std::priority_queue<int, std::vector<int>, std::greater<int> > q2;
for(int n : {1,8,5,6,3,4,0,9,7,2})
q2.push(n);
print_queue(q2);
// Using lambda to compare elements.
auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
for(int n : {1,8,5,6,3,4,0,9,7,2})
q3.push(n);
print_queue(q3);
}