Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I store 3 integer in priority_queue?

I want to store 3 integer in priority_queue. I know how to store 2 integer. I store 2 integer with pair<int,int>

my code

priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq;
pq.push(make_pair(5,6));

but I don't know how can I store 3 integer. I need help.

sorry for my English.

like image 511
Elmi Ahmadov Avatar asked Apr 19 '11 05:04

Elmi Ahmadov


2 Answers

Simplest would be create a struct which logically binds all the integers and create a priority queue of that struct objects.

EDIT Sample code:

#include <queue>
using namespace std;
struct S
{
    int m_n1;
    int m_n2;
    int m_n3;

    S(int n1, int n2, int n3) : m_n1(n1), m_n2(n2), m_n3(n3)
    {
    }

    bool operator<(const struct S& other) const
    {
        //Your priority logic goes here
        return m_n1 < other.m_n1;
    }
};

int main()
{
    priority_queue<S> pq;

    //Add the elements to the queue
    pq.push(S(1,2,3));
    pq.push(S(4,2,3));
    pq.push(S(2,2,3));

    //This element will be S(4,2,3)
    S s1 = pq.top();
    pq.pop();

    return 0;
}
like image 77
Naveen Avatar answered Sep 29 '22 06:09

Naveen


or the easy way: std::pair<int,std::pair<int,int>>

like image 44
cprogrammer Avatar answered Sep 29 '22 07:09

cprogrammer