I cannot seem to insert items without confusing errors I do not understand.
I want a lists or ints stored by strings to test this:
multimap<string, int> Map;
Printable View
I cannot seem to insert items without confusing errors I do not understand.
I want a lists or ints stored by strings to test this:
multimap<string, int> Map;
PHP Code:Map.insert(pair<string, int>("a", 5));
Map.insert(pair<string, int>("a", 6));
Map.insert(pair<string, int>("a", 7));
Map.insert(pair<string, int>("b", 8));
Map.insert(pair<string, int>("b", 9));
Map.insert(pair<string, int>("b", 0));
std::multimap<string, int>::iterator itr = Map.lower_bound("a");
for(itr; itr != Map.upper_bound("a"); itr++)
{
std::cout << "value: " << itr->second << std::endl;
}
for(itr; itr != Map.upper_bound("b"); itr++)
{
std::cout << "value: " << itr->second << std::endl;
}
Do I have the idea?
That looks almost right, only:
- you don't want to call uper_bound every iteration of the loop, since it has to do a lookup in the multimap
- itr could be invalid inside the second loop, of course it isn't because you just initialized the data, but if there were no "a"s, then itr == Map.end() != Map.upper_bound("b").
- instead of calling an explicit std::pair constructor you can also use the std::make_pair function, it deduces the template arguments
Okay, those are all noted.
Now, as for the comparisons, is there a quick way to have the multimap do comparisons based on keys.
Lets say the key is "a" and I wanna know if 10 exists in the "a" key.
Would I iterate through like above and just do a comparison?
Or is there a function provided for that?
As for the last post I made I am also having trouble removing one element from a key.
map.erase("a"); -- erase's all elements in the "a" key
map.erase(map.begin(), map.end()) -- _FIRST, _LAST -- erase's whole map
now lets say my map has
"a" -- 1,2,3,4,5,6,7,8,9,0
I want to erase 5.
map.erase(map.lower_bound("a"), _____);
See I am not sure on the best way to remove the fifth element except to search through the keys and find it.
In both cases a loop from lower_bound to upper_bound is the best option. If this is something you will be doing a lot then perhaps multimap is not the right container. A multimap usually means that you don't really care about which value you get with a specific key, they are all equall.
You could try std::map<string, std::set<int> > (not sure how well it would perform), or std::set<std::pair<string, int> >
It is something I need to do a lot.
Each message put into a queue will come with a Key, then the key is searched for a the message, if the message exists then that message will transmit to the Key associated with the objects.
So, I do not think anything else will be much faster, unless there is a function which simply goes check(dwKey, dwMsg) -- returns true if the msg is in the key.
Also, again, how can I remove a single element from a key like in the post above?
As far as I know you have to use a loop to remove a specific key/value combination:
Code:typedef std::multimap<std::string, int> Messages;
Messages messages;
string to_remove_key = ...;
int to_remove_value = ...;
Messages::iterator it = messages.lower_bound(to_remove_key);
Messages::iterator end = messages.upper_bound(to_remove_key);
while (it != end) {
if (it->second == to_remove_value) {
Messages::iterator to_remove = it;
++it; // iterator becomes invalid on erase
messages.erase(to_remove);
} else {
++it;
}
}