Jump to content

Small question on containers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Frenzy123

Frenzy123

    Newbie

  • Members
  • PipPip
  • 15 posts
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 :)

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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
Frenzy123

Frenzy123

    Newbie

  • Members
  • PipPip
  • 15 posts
Thx for that void. A nice clear response :D