This code produces an error:
priority_queue<int, vector<int>, [](int a, int b)->bool{return a>b;}> q;
Why?
(I know that for things like this I can just use std::greater or the default sorting, but I'm trying to learn how to create a custom comparator)
2 errors generated:
error: no matching function for call to object of type lambda error: template argument for template type parameter must be a type
You need to specify the type, but not the lambda expression itself as the template argument. And lambda should be specified as constructor argument.
E.g.
auto c = [](int a, int b)->bool{return a>b;}; // declare lambda in advance
priority_queue<int, vector<int>, decltype(c)> q(c);
// ^^^^^^^^^^^ <- specify the type of lambda
// ^ <- specify the lambda as constructor argument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With