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.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
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.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
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.
StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138