My question is how does the priority Queue sort the Blocks *? More specifically how can I have it give one Block* a high priority solely base on the time variable? (I hope this is clear)
#include <iostream>
#include <queue>
#include <ctime>
#include <cstdlib>
struct data
{
int x;
int y;
data(int x, int y) : x(x), y(y) {}
};
std::ostream& operator<<(std::ostream& out, const data& d)
{ return out << d.x << " " << d.y; }
struct comp_data
{
booloperator() (const data* d1, const data* d2) const
{
if (d1->x == d2->x)
return d1->y < d2->y;
return d1->x < d2->x;
}
};
int main (int argc, char **argv)
{
srand(time(NULL));
std::priority_queue<data*, std::vector<data*>, comp_data> p_queue;
for (unsignedint i = 0; i < 20; i++)
p_queue.push(new data(rand() % 10,rand() % 10));
while (!p_queue.empty())
{
std::cout << *p_queue.top() << '\n';;
delete p_queue.top();
p_queue.pop();
}
return 0;
}
Maybe I could explain. If you had done this: priority_queue<block> p_queue;
You would get a very long list of errors which all stem from there being no operator< for block. You could overload this operator if you wanted.
When you do this: priority_queue<block*> p_queue;
You don't get any errors because the operator< is acting upon memory locations. It is a better idea to make a cmp_blocks, or comp_data, than to overload the < operator when dealing with pointers.
You have to add std::vector<block*> in there because it's simply the first of two deafault values in the priotity_queue's constructor.