Thread: 2 vectors vs multimaps

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    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?

    Thanx in advance

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    One more question

    I also wanted know how the performance would be affected if I use a map with message id as key and a list containing the delegates as the value....

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Will there be any major degradation in performance if I use multimaps?
    Maybe not, but you could run some tests.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    Structure is fine but..

    Quote Originally Posted by matsp View Post
    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...

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM