C++ priority_queue and comparator
December 15, 2021
std::greater or less
priority_queue<int,vector<int>,greater<int>> pq;
use function
bool compare(ListNode* a, ListNode* b)
{
return (a->val > b->val);
}
priority_queue<ListNode*, vector<ListNode*>, decltype(&compare)> q(compare);
using lambda to compare
auto compare = [](int lhs, int rhs)
{
return lhs < rhs;
};
std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);
struct
struct Compare
{
bool operator()(const int& lhs, const int& rhs)
{
return lhs < rhs;
}
};
priority_queue<int,vector<int>, Compare > pq;