2 vectors vs multimaps

This is a discussion on 2 vectors vs multimaps within the C++ Programming forums, part of the General Programming Boards category; Hi
I am currently writing a message handling module which uses delegates. I am maintaining 2 vectors, one for message ...

2 vectors vs multimaps

Hi

I am currently writing a message handling module which uses delegates. I am maintaining 2 vectors, one for message ids and the other one for the delegate (pointer to the function to be called). When I receive a message I check the first vector to get the indices for which the message id is registered. I pick the delegates @ the same indices from the second vector and invoke them.
Since there can be multiple methods registering for the same id, I cannot use maps or hashmaps. Will there be any major degradation in performance if I use multimaps?

Is there something preventing you from using a struct as the vector content, so that you can search the vector for your message id, and immediately have the corresponding delegate function? Seems like the obvious solution to me.

Structure is fine but..

Originally Posted by matsp

Is there something preventing you from using a struct as the vector content, so that you can search the vector for your message id, and immediately have the corresponding delegate function? Seems like the obvious solution to me.

--
Mats

But if I use a structure how will I perform a search?? In that case I will have to serially check each of the elements in the vector for the message id.. If the match is towards the end of the vector, it wouldn't be making much of a difference , would it? Or maybe it would because the number of elements in the list reduces...

Indeed, you can't use the std::vector::find() function - however, find is not "magical" - unless the vector is sorted, it will have to walk through the vector one element at a time anyways. So the only difference is that you have to write the search function yourself.

Of course, if the message ID's are sorted in order, you could make a binary search, which means ten compares for 1000 message ID's, 20 compares for 1000000 message ID's.

Of course, if the message ID's are sorted in order, you could make a binary search, which means ten compares for 1000 message ID's, 20 compares for 1000000 message ID's.

In that case, one may be better off just using multimap. In my opinion, the choice depends on whether you need to keep the container sorted due to frequent insertions and searches (in which case an associative container would be good), or if you can do a block of insertions, then a single sort, and then whatever searches you need (in which case a vector would be good).