### Priority queue

Hello i want to construct a comparison to have priority queue's top according to the biggest first number on a pair(i have defined pair of integers as pii) of integers
 ``12345678910`` ``````struct comp//construct { bool operator()(const pii& a,const pii& b) { return (a.first>b.first); } }; priority_queue,comp> pq;``````

how can i fix this?
Thank you all
Don't forget to use the `second` number in case the `first`s are equal.
 ``123456789`` ``````struct Comp { bool operator () (const pii &a, const pii &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } };``````
> i want to construct a comparison to have priority queue's top
> according to the biggest first number on a pair

You don't have to do anything; the operator< for `std::pair<>` does precisely that.
http://en.cppreference.com/w/cpp/utility/pair/operator_cmp

 ``12345678910111213141516171819202122`` ``````#include #include #include int main() { using pii = std::pair< int, std::string > ; std::priority_queue pq ; pq.emplace( 45, "abc" ) ; pq.emplace( 78, "abc" ) ; pq.emplace( 12, "xyz" ) ; pq.emplace( 25, "pqr" ) ; pq.emplace( 45, "def" ) ; while( !pq.empty() ) { const auto& pair = pq.top() ; std::cout << pair.first << ',' << pair.second << '\n' ; pq.pop() ; } }``````
Thank you very very much :)
Topic archived. No new replies allowed.