6

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.

Elmi Ahmadov
  • 1,017
  • 5
  • 14
  • 25

3 Answers3

8

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;
}
Naveen
  • 74,600
  • 47
  • 176
  • 233
  • Either `operator<` or a Compare functor would have to be specified for `priority_queue` to work with this struct. – Emile Cormier Apr 19 '11 at 06:10
  • I create struct but I can't push integers. how can I push integers? – Elmi Ahmadov Apr 19 '11 at 06:10
  • Since priority queue sorts the elements so that the greatest is always the first. In my comparison operator `operator <` of `struct S` I have compared the elements based on the `int m_n1;`. Since of the 3 structs I added to the queue `S(4,2,3)` is having the highest value for this integer it is coming first. – Naveen Apr 19 '11 at 06:28
  • @Elmi: You might want to do a *lexicographical* compare. This is the type of comparison done with std::pair. Check out http://stackoverflow.com/questions/2500664 . – Emile Cormier Apr 19 '11 at 07:12
5

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

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • -1 `triplet.second.second` is a terribly ugly and convoluted way of accessing the third element of the triplet. This solution does not scale well at all. – Emile Cormier Apr 19 '11 at 06:39
  • it shouldn't scale and anyway is not less scalable the other solutions (excluding boost/tr1 tuple). And ... it's the fastest one :) (as writing code). About the ugly ... it;'s just doing the job. Just my opinion – cprogrammer Apr 19 '11 at 13:07
  • 3
    cprogrammer, this is much better than the chosen solution for certain applications: eg competitive programming. – Evan Weissburg Jan 20 '18 at 00:59
3

You may use Boost::Tuple

#include "boost/tuple/tuple.hpp"
#include <queue>
#include <vector>
#include <iostream>

typedef boost::tuple<int, int, int> triple_t;

class my_greater  {
public:
  bool operator() (const triple_t& arg1, const triple_t& arg2) const
  {
    return arg1.get<0>() > arg2.get<0>();
    return false;
  }
};

typedef std::priority_queue<triple_t, std::vector<triple_t>, my_greater> 
   my_priority_queue;

int main()
{
  my_priority_queue triples;

  triples.push(boost::make_tuple(1,2,3));
  triples.push(boost::make_tuple(10,20,30));
  triples.push(boost::make_tuple(5,10,15));
  triples.push(boost::make_tuple(15,30,45));
  triples.push(boost::make_tuple(2,4,6));

  std::cout << "Result: \n";
  while (!triples.empty()) 
  {
    const triple_t& t = triples.top();
    std::cout << t.get<0>() << ", " << t.get<1>() << ", " << t.get<2>() << std::endl;
    triples.pop();
  }

  return 0;
}