Skip to main content
gamedev.net
🔒 Locked

Priority queues

Started by Penguinzchaseme Jul 7, 2004 at 5:34 PM 0 replies 650+ views
Original Post
Penguinzchaseme
Penguinzchaseme
Can someone show me a working example of a priority queue in C++ because I don't know the proper syntax. I need it to support a comparison function for a userdefined structure. thanx!
When Penguins are chasing you, resistance is futile, give into their demands and they may spare you
SiCrane
SiCrane
Here's an example:
struct SomeStruct {  int i;  float f;};struct Sorter {  bool operator()(const SomeStruct & lhs, const SomeStruct & rhs) {    if (lhs.i < rhs.i) return true;    if (lhs.i > rhs.i) return false;    if (rhs.f > rhs.f) return true;    return false;  }};int main(int, char **) {  std::priority_queue<SomeStruct, std::vector<SomeStruct>, Sorter> p_queue;  SomeStruct a = { 0, 0.0f };  SomeStruct b = { 1, 1.0f };  p_queue.push(a);  p_queue.push(b);    SomeStruct c = p_queue.top();  return 0;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.