| | |
priority_queue help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
I'm trying to create a central event system for my game where I add some events to a priority_queue and pull off the first one as soon as it expires. But I'm having a problem of understanding if I'm adding them to the queue correctly, if they're in order, or how to pull them off. Can anyone lend a hand here?
Initialize the queue.
Here I create 10 events and put them on a queue after assigning the current timestamp, and then the later timestamp to execute this event.
Then in each frame of my game, i want to check the queue to see if the event is ready to be executed.
Here are my event classes ...
Can anyone help me use priority_queue's correctly?
Initialize the queue.
C++ Syntax (Toggle Plain Text)
my_queue = new MyEventQueue();
Here I create 10 events and put them on a queue after assigning the current timestamp, and then the later timestamp to execute this event.
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 10; i++) { MyEvent *event = new MyEvent(); event->set_start_timestamp(); // now time event->set_execute_timestamp(); // time to execute from now my_queue->push_event(event); }
Then in each frame of my game, i want to check the queue to see if the event is ready to be executed.
C++ Syntax (Toggle Plain Text)
if (my_queue->count() > 0) { MyEvent *evnt = my_queue->top_event(); // is_expired() is how i was doing it, but didn't work if (evnt->is_expired()) { execute_event(evnt->id()); my_queue->pop_event(); } }
Here are my event classes ...
C++ Syntax (Toggle Plain Text)
class MyEvent { ... public: ... bool is_expired(); struct timeval start_timestamp(); double execute_timestamp(); }; struct EventPointerCompare { bool operator() (MyEvent* lhs, MyEvent* rhs) const { return ( lhs->_execute_time < rhs->_execute_time ); } }; // this is my priority_queue wrapper class MyEventQueue { private: priority_queue<MyEvent *, vector<MyEvent *>, EventPointerCompare> event_queue; public: check_queue(); has_events_of_type_x(); ...
Can anyone help me use priority_queue's correctly?
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
I am a little confused by your queue implementation. Usually your queue functions would be enqueue to add the elements to the queue, and dequeue to get the first element you added off the queue, simulating FIFO behavior.
A stack on the other hand uses push and pop for LIFO
When you do this
A stack on the other hand uses push and pop for LIFO
When you do this
MyEvent *evnt = my_queue->top_event(); , are you getting the first element you added or the last element you added ? If its the last element its likely that the evnt->is_expired() won't work. Last edited by stilllearning; Oct 22nd, 2008 at 2:08 am.
•
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
I am a little confused by your queue implementation. Usually your queue functions would be enqueue to add the elements to the queue, and dequeue to get the first element you added off the queue, simulating FIFO behavior.
A stack on the other hand uses push and pop for LIFO
When you do thisMyEvent *evnt = my_queue->top_event();, are you getting the first element you added or the last element you added ? If its the last element its likely that the evnt->is_expired() won't work.
So this just calls top() but I don't think this will work which is why I started to overload the operator() to get the next timestamp in the queue w/ priority of the least amount of time.
MyEvent *evnt = my_queue->top_event(); •
•
Join Date: Oct 2008
Posts: 4
Reputation:
Solved Threads: 0
That's one way, but for a priority_queue, I don't think that's how it should work, otherwise I could use a regular queue if I just placed them on the queue in the order I need. I believe a priority_queue should actually pull off the item in the queue based on it's "priority" or my overriding implementation of the item w/ the next 'least' amount of time. However, I'm having a hardtime understanding the implementation of it.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Input Stream Questions
- Next Thread: Linked list implementation of a queue help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





