Hi guys was wondering if you could tell me the difference between maps and multimaps. I have used maps before but i have never come across multimap's before. Thx :)
Small question on containers
Started by Frenzy123, Feb 18 2008 12:47 AM
2 replies to this topic
#1
Posted 18 February 2008 - 12:47 AM
|
|
|
#2
Posted 18 February 2008 - 01:16 AM
If you're using multimaps you're able to have two or more entries by the same name, but with different values.
std::multimap<std::string, int> MyMultiMap;
MyMultiMap.insert(std::pair<std::string, int>("Key1", 11));
MyMultiMap.insert(std::pair<std::string, int>("Key2", 21));
MyMultiMap.insert(std::pair<std::string, int>("Key1", 12));
for(std::multimap<std::string, int>::iterator it = MyMultiMap.begin();
it != MyMultiMap.end();
it++)
{
std::cout << "Name: " << it->first << std::endl;
std::cout << "Value: " << it->second << std::endl;
std::cout << std::endl;
}
You can verify that there's multiple entries with the same name by using count.
std::cout << "Amount of 'Key1': " << MyMultiMap.count("Key1") << std::endl;
#3
Posted 19 February 2008 - 03:32 PM
Thx for that void. A nice clear response :D


Sign In
Create Account


Back to top









