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?
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?
You can do something like this:
or 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; } }
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
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.
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.
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:for (pos = cont.begin(); pos != cont.end(); ++pos) { if (pos->second == word) { cout << " " << pos->second << endl; } }
Also note he's looking for keys, not values. The checking should be done through pos->firstCode: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; }
Last edited by Mario F.; 08-11-2006 at 07:33 AM.
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.
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
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.