Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to use comparator funtion in priority queue in c++

// suppose you want to apply comparator on pair <int, string>
// declaration of priority queue
priority_queue<pair<int, string>, vector<pair<int, string>>, myComp> pq;
// here myComp is comparator
struct myComp {
  bool operator()(
    pair<int, string>& a,
    pair<int, string>& b)
  {
    return a.first > b.first;
    // can write more code if required. depends upon requirement.
  }
};
// another way 
 auto compare = [](int lhs, int rhs){
  return lhs < rhs;
};

priority_queue<int, vector<int>, decltype(compare) > pq(compare);
Comment

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;
}
Comment

comparator priority queue c++


struct CompareHeight {
    bool operator()(Person const& p1, Person const& p2)
    {
        // return "true" if "p1" is ordered
        // before "p2", for example:
        return p1.height < p2.height;
    }
};
 
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to speed up cin and cout 
Cpp :: set cmd size c++ 
Cpp :: return the index where maximum element in a vector 
Cpp :: access last element in vector in c++ 
Cpp :: c++ run loop for 5 seconds 
Cpp :: taking user input for a vector in c++ 
Cpp :: HOW TO TURN LINK TO BUTTON IN MVC 
Cpp :: how to get the player view point location and rotation in ue4 c++ 
Cpp :: if not defined c++ 
Cpp :: qt qstring to double 
Cpp :: prime number program in c 
Cpp :: c++ main environment variables 
Cpp :: how to print with the bool value in cpp 
Cpp :: c++ vector element search 
Cpp :: c++ print every element in array 
Cpp :: default access modifier in c++ in struct 
Cpp :: map defualt value c++ 
Cpp :: cpp split string by space 
Cpp :: how to run a c++ program in the background 
Cpp :: how to iterater map of sets in c++ 
Cpp :: count word accurances in a string c++ 
Cpp :: copy array c++ 
Cpp :: sort vector in descending order 
Cpp :: tuple c++ 
Cpp :: iteraate through a vector 
Cpp :: c++ 2d vector assign value 
Cpp :: convert refference to pointer c++ 
Cpp :: How to pause a c++ program. 
Cpp :: strlen in c++ 
Cpp :: c++ check palindrome 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =