Jump to content
  • Advertisement
Sign in to follow this  
Penguinzchaseme

Priority queues

This topic is 5071 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!