J
Joe, G.I.
Can anyone help me w/ a priority_queue. I'm generating MyEvent classes
and I put them on a priority_queue, but I don't know how to get them in
priority. The priority is the event w/ the smallest timestamp.
// just a wrapper around priority_queue
pq = new MyPriorityQueue();
// I generate 10 events w/ a random timestamp
for (int i = 0; i < 10; i++) {
MyEvent *event = new MyEvent();
event->set_id(idx++);
event->set_gen_tstamp();
pq->push_event(event);
}
// Now I need to find the event w/ the smallest timestamp
if (pq->size() > 0) {
MyEvent *evnt = pq->top_event();
if (evnt->is_expired()) {
// do stuff ... then remove this event
pq->pop_event();
}
}
// Not sure what I'm doing here, but I'm trying to do an overload
// operator and have it return the priority of the smallest time. I
// think this is where I need help.
// Not even sure if this is the thing to do.
bool MyEvent:perator < (const MyEvent *event)
{
if (_timestamp < event->_timestamp())
return true;
}
return false;
}
and I put them on a priority_queue, but I don't know how to get them in
priority. The priority is the event w/ the smallest timestamp.
// just a wrapper around priority_queue
pq = new MyPriorityQueue();
// I generate 10 events w/ a random timestamp
for (int i = 0; i < 10; i++) {
MyEvent *event = new MyEvent();
event->set_id(idx++);
event->set_gen_tstamp();
pq->push_event(event);
}
// Now I need to find the event w/ the smallest timestamp
if (pq->size() > 0) {
MyEvent *evnt = pq->top_event();
if (evnt->is_expired()) {
// do stuff ... then remove this event
pq->pop_event();
}
}
// Not sure what I'm doing here, but I'm trying to do an overload
// operator and have it return the priority of the smallest time. I
// think this is where I need help.
// Not even sure if this is the thing to do.
bool MyEvent:perator < (const MyEvent *event)
{
if (_timestamp < event->_timestamp())
return true;
}
return false;
}