c++ give priority queue a comparator code example
Example 1: comparator for priority queue c++
class Foo
{
};
class Compare
{
public:
bool operator() (Foo, Foo)
{
return true;
}
};
int main()
{
std::priority_queue<Foo, std::vector<Foo>, Compare> pq;
return 0;
}
Example 2: how to use priority queue comparator stl c++
void SamplePriorityQueueWithLamda()
{
auto compare = [](int lhs, int rhs)
{
return lhs < rhs;
};
std::priority_queue<int, std::vector<int>, decltype(compare)> q(compare);
for(int n : {1,8,5,6,3,4,0,9,7,2})
q.push(n);
printQueue(q);
}