Jump to content
  • Advertisement
Sign in to follow this  
Deathscythe

Best way to iterate through a multimap

This topic is 5146 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

What is the best way to iterate through a multimap with a given key? Here is my example. I have a multimap where the Key is a set of possible Events and the values are Callback functions that are registered to those events. When an event is fired, I'd like to retrieve a list of elements whose key matches the given event. I know I have get the first element with a given key, but I need all of them. Right now I do this. for(each element in multimap) { get ith element if(ith element has matching key) call the callback function } I am using std::multimap and MS Visual Studio 2003 (7.1 I think). Does multimap provide a function to accomplish this? or is there just a better way? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
EDIT: Ignore my fallicy, Washu is right!

Actually, I've got some spare time to post why I originally recommended the OP's method. In short, it's legal [though not a good idea] to provide a BinaryPredicate that leaves a map or multimap unsorted [by key]. Sometimes, it's even madatory; occaisons may arise when f(x) != f(y) even when x = y. On these occaisons, equal_range won't work, and you're left with no option but to manually search.

[Edited by - Chris Hare on September 13, 2004 12:10:30 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
Actually, I've got some spare time to post why I originally recommended the OP's method. In short, it's legal [though not a good idea] to provide a BinaryPredicate that leaves a map or multimap unsorted [by key]. Sometimes, it's even madatory; occaisons may arise when f(x) != f(y) even when x = y. On these occaisons, equal_range won't work, and you're left with no option but to manually search.

Very bad idea. If you have a tree, and x = y, then f(x) should be f(y). Otherwise you have a serious problem, and the tree could end up malformed. This is a case where writing a specialized container is the correct approach. The STL isn't meant to solve every problem.
Quote:
Original post by Chris Hare
meh

meh.

Share this post


Link to post
Share on other sites
Of course, but for occaisons when the structure of the tree is irrelevant, I've found it most convenient to use map and multimap in such a vein.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris Hare
Of course, but for occaisons when the structure of the tree is irrelevant, I've found it most convenient to use map and multimap in such a vein.

Malformed as in inserts will break, searches may fail, etc. The standard is very clear on the fact that if x = y, then !comp(x, y) == false && !comp(y, x) == false

In other words, breaking that rule can result in Bad Things™

Share this post


Link to post
Share on other sites
Thanks Washu, equal_range works great. Now I iterate like this:
IFunctor* handler;
std::pair <EventMap::iterator, EventMap::iterator> it;
it = m_Callbacks.equal_range(Event.type);
EventMap::iterator end = it.second;
for(EventMap::iterator begin = it.first; begin != end; begin++)
{
handler = begin->second;
(*handler)(Event);
}


This works out much better epescially as I register more callbacks.

[Edited by - Washu on September 13, 2004 11:41:30 AM]

Share this post


Link to post
Share on other sites
Also, using std::for_each (as petewood said before he deleted his post) can help to cleanup that code somewhat.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!