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
1
2
3
4
5
6
7
8
9
10
struct comp//construct
{
bool operator()(const pii& a,const pii& b)
{
return (a.first>b.first);
}

};

priority_queue<pii,vector<pii>,comp> pq;

how can i fix this?
Thank you all
Don't forget to use the second number in case the firsts are equal.
1
2
3
4
5
6
7
8
9
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <queue>
#include <string>
#include <iostream>

int main()
{
    using pii = std::pair< int, std::string > ;
    std::priority_queue<pii> 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.