Priority Queue Comparison

后端 未结 6 1768
心在旅途
心在旅途 2021-02-05 08:40

I\'m trying to declare a priority queue in c++ using a custom comparison function...

So , I declare the queue as follows:

std::priority_queue

        
6条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 09:24

    The template parameter should be the type of the comparison function. The function is then either default-constructed or you pass a function in the constructor of priority_queue. So try either

    std::priority_queue, decltype(&compare)> pq(&compare);
    

    or don't use function pointers but instead a functor from the standard library which then can be default-constructed, eliminating the need of passing an instance in the constructor:

    std::priority_queue, std::less > pq;
    

    http://ideone.com/KDOkJf

    If your comparison function can't be expressed using standard library functors (in case you use custom classes in the priority queue), I recommend writing a custom functor class, or use a lambda.

提交回复
热议问题