stl multimap results

This is a discussion on stl multimap results within the C++ Programming forums, part of the General Programming Boards category; I use a multimap with values and key's. When I search for a key I might get multiple results (multimap). ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    stl multimap results

    I use a multimap with values and key's. When I search for a key I might get multiple results (multimap). How do I do that? I know how to search for one item using the key value. How do you do that with multiple results? How do you store that?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    You can do something like this:
    Code:
    ...
    multimap <string, int> cont;
    ...
    string word = "test";
    
    for (pos = cont.begin(); pos != cont.end(); ++pos) {
               if (pos->second == word) {
                   cout << " " << pos->second << endl;
               }
           }
    or something like this:
    Code:
    for (it = cont.lower_bound(word); it != cont.upper_bound(word); ++it)
    {
                   cout << it->second << endl;
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    You can use lower_bound() and upper_bound(). Read about those. The first return an iterator to the first element with a given key, and the other returns an iterator one-past-the-last element with that key. So, in all effect you have an iterator range.

    You can also use a combination of find() and count(), knowing that despite the fact multimaps allow for multiple keys of the same value, at insertion elements are still sorted according to their key values.

    So finding all elements with a given key is a simple matter of computing find() and then iterating the multimap count() - 1 times.

    You can also use equal_range. It returns a pair of iterators with .first and .second giving you also an iterator range. If the key is not present, .first and .second hold the position where that key could have been inserted (very valuable if you are working with insert iterators). So, to test if anything has been found is just a matter of testing if .first and .second are equal. If they are, then nothing was found.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Code:
    for (pos = cont.begin(); pos != cont.end(); ++pos) {
               if (pos->second == word) {
                   cout << " " << pos->second << endl;
               }
           }
    That's a possibility yes, albeit the slowest. Also, since multimaps are automatically sorted for us, I would add a flag to avoid iterating the whole of the container.

    Code:
    bool found_ = false;
    for (pos = cont.begin(); pos != cont.end(); ++pos) {
               if (pos->second == word) {
                   found_ = true;
                   cout << " " << pos->second << endl;
               } else if(found_) break;
    }
    Also note he's looking for keys, not values. The checking should be done through pos->first
    Last edited by Mario F.; 08-11-2006 at 08:33 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Hehe Mario, I have shown how this can be done. Also I wrote code for diplaying all values with specific key. I think he's smart enough to modify the code to fit his needs.
    Cheers
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use equal_range for this.

    It's better than lower_bound and upper_bound because it does both in a single call, and can therefore be faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL multimap error
    By 7stud in forum C++ Programming
    Replies: 12
    Last Post: 11-23-2005, 12:48 AM
  2. STL multimaps / searching
    By Codulation in forum C++ Programming
    Replies: 7
    Last Post: 01-05-2004, 06:28 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  5. Unsorted Map and Multimap :: STL
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2002, 11:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21