As we know priority queue is known for its first element is always the greatest of the elements it contains but how can I make the first element to be the lowest element of them all?

// priority_queue::push/pop
#include <iostream>
#include <queue>
using namespace std;

int main ()
{
  priority_queue<int> mypq;

  mypq.push(30);
  mypq.push(100);
  mypq.push(25);
  mypq.push(40);

  cout << "Popping out elements...";
  while (!mypq.empty())
  {
     cout << " " << mypq.top();
     mypq.pop();
  }
  cout << endl;


  system("PAUSE"); 
  return 0;
}

like this one,I want to make 25 to be the first one to be pop out and its gonna be the one on the top element

Recommended Answers

All 7 Replies

Any online reference to priority_queue
http://www.cplusplus.com/reference/stl/priority_queue/ and the include show that the class takes a templated less functor and a default typebase.

So: (without using default template parameters we have):

#include <iostream>
#include <vector>
#include <queue>

int main ()
{
  std::priority_queue<int,std::vector<int>,std::less<int> > pqA;
  std::priority_queue<int,std::vector<int>,std::greater<int> > pqB;

  pqA.push(30);   pqB.push(30);
  pqA.push(100);  pqB.push(100);
  pqA.push(25);   pqB.push(25);
  pqA.push(40);   pqB.push(40);

  std::cout << "Popping out elements..."<<std::endl;
  while (!pqA.empty())
  {
    std::cout << " " << pqA.top() <<" "<<pqB.top()<<std::endl;
    pqA.pop();
    pqB.pop();
  }
  return 0;
}

You can replace less/greater with your own functional if you like, and you can replace vector with any container that has a random access iterator.

oh..thanks

Im just curious about this....Is this possible,Ive tried it but I got some error

#include <iostream.h>
#include <vector.h>
#include <queue.h>


struct data{
  int year;
  int n;
};


int main ()
{
    priority_queue<int,vector<int>,less<int> > pq;
   
    data m;
    m.year = 19;
    m.n = 12;
    pq.push(data);
   
    m.year = 119;
    m.n = 22;
    pq.push(data);
   
    cout<<"Smallest data"<<pq.top(); 
 
                          
  system("PAUSE");
  return 0;
}

basically I just want to make the first element the one that I push first (line 19)

What you have done is perfectly doable, but not the way you have done it.

First: Why yours is wrong: priority_queue<int,vector<int>,less<int> > pq; We see here that priority_queue is storing type of int. BUT at lines 19 and 22 you are pushing a struct of type data.

But you can easily do this

#include <iostream>
#include <vector>
#include <list>
#include <queue>

struct data
{
  int year;
  int n;
};


class compGT
{
public:

  bool operator()(const data& A,const data& B) const
    { return A.year>B.year; }
};

int main()
{
  std::priority_queue<data,std::vector<data>,compGT> pqC;
  data m;
  m.year = 19;
  m.n = 12;
  pqC.push(m);
   
  m.year = 119;
  m.n = 22;
  pqC.push(m);
  
  std::cout<<"Smallest data:"<<pqC.top().year<<" "
	   <<pqC.top().n<<std::endl; 

  return 0;
}

I have used an external comparitor functional but you can just as easily use an interal operator<. You can compare on both year and n or some complex function should you wish.

I have fixed a number of basic errors, that is beginning to tell me that you are getting a little out of your depth. That is ok, but remember to be continuously thinking about the basics first. That means REALLY reading the error messages your compiler is giving you.

(i) You return a pcC.top() item to the operator<< but don't provide an operator<< that works with data.

(ii) You push back a type (data) rather than an object m.

okay now I get it..oh I was so lame..thanks man.. I really appreciate your help

another question again...how about a 3d array can I use it in here in the priority queue and then it will make the top element to be smallest 3d array values
lets say my 3d array is
struct
{
int maze[x][y][cost];
}

Im not comfortable with the 3d array concept,but I want to just give it a try,so can anyone help me with this?

The anwer is exactly as above, assuming that you want to put it into a structure. If you are not then you will have to write a less comparitor for it.

There is no fundermental different between your struct data , and your struct containing maze, in terms of the priority_queue.

The only problem is the coping of the class. You have to be careful about (a) the number of copies and (b) the way the copy is done.
If maze is big. then you are better storing a pointers to the structures. If it is small, be sure to write the copy/assignment operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.