dimanche 1 août 2021

priority queue with lambda syntax is confusing

As per specification of priority queue

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;

But why this odd syntax with Lambda?

// 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);

Why we need to pass cmp as an argument?

Aucun commentaire:

Enregistrer un commentaire