Sign in to follow this  

Best way to iterate through a multimap

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

This topic is 4843 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this