104

I found that they have one key and multiple values which is unique.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
superK
  • 3,932
  • 6
  • 30
  • 54

4 Answers4

85

A std::map is an associative container, that allows you to have a unique key associated with your type value. For example,

void someFunction()
{
    typedef std::map<std::string, int> MapType;
    MapType myMap;

    // insertion
    myMap.insert(MapType::value_type("test", 42));
    myMap.insert(MapType::value_type("other-test", 0));

    // search
    auto it = myMap.find("test");
    if (it != myMap.end())
        std::cout << "value for " << it->first << " is " << it->second << std::endl;
    else
        std::cout << "value not found" << std::endl;
}

A std::multimap is equal to a std::map, but your keys are not unique anymore. Therefore you can find a range of items instead of just find one unique item. For example,

void someFunction()
{
    typedef std::multimap<std::string, int> MapType;
    MapType myMap;

    // insertion
    myMap.insert(MapType::value_type("test", 42));
    myMap.insert(MapType::value_type("test", 45));
    myMap.insert(MapType::value_type("other-test", 0));

    // search
    std::pair<auto first, auto second> range = myMap.equal_range("test");
    for (auto it = range.first; it != range.second; ++it)
        std::cout << "value for " << it->first << " can be " << it->second << std::endl;
}

The std::set is like an std::map, but it is not storing a key associated to a value. It stores only the key type, and assures you that it is unique within the set.

You also have the std::multiset, that follows the same pattern.

All these containers provide an O(log(n)) access with their find / equal_range.

typedef
  • 1,159
  • 1
  • 6
  • 11
  • 6
    In multimap function, this line `std::pair range = myMap.equal_range("test");` doesn't work, because `error: 'auto' not allowed in template argument`. Use `const auto range = myMap.equal_range("test")` instead. – vancexu Mar 02 '14 at 17:15
  • 2
    mapType? Shouldn't it be MapType on line 4? – lolololol ol Oct 17 '16 at 15:16
  • not sure who was first, but one is obviously a copy paste of the other : https://www.cppbuzz.com/What-is-difference-between-map-and-multimap – 463035818_is_not_an_ai Jan 11 '18 at 14:07
  • 1
    ahah, cppbuzz is scraping StackOverflow or what?, I wrote this answer myself years ago when I was still daily coding in c++. And there is indeed a typo line 4, thanks @lololololol – typedef Jan 12 '18 at 10:46
  • 1
    (and their copy/paste failed, they do not even display types in template std::map declaration : std::map) – typedef Jan 12 '18 at 10:54
  • @typedef Someone else scraping your work is a serious problem. In such cases, the correct action is to use the "contact us" form. See https://meta.stackexchange.com/a/200178. – L. F. Jul 27 '19 at 02:38
58

The multimap stores pairs of (key, value) where both key and value can appear several times.

The map<key, set<value>> will only store each value once for a specific key. To do that, it will have to be able to compare the values, not just the keys.

It depends on your application if the values that compare equal are equivalent, or if you wish to store them separately anyway. Perhaps they contain fields that are different but do not take part in the comparison for the set.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 6
    So, a std::multimap is like a std::map >, the difference between them is that the later's values are sorted. Is that right? – superK Dec 22 '11 at 15:56
  • 3
    No, `std::multimap` allows the same key to appear multiple times whereas `std::map` requires the uniqueness of `key`. – Yixing Liu Jul 13 '18 at 15:25
14
map::insert

Because map containers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value if so, the element is not inserted and its mapped value is not changed in any way.

on the other hand

multimap::insert 

can insert any number of items with the same key.

http://www.cplusplus.com/reference/stl/map/
http://www.cplusplus.com/reference/stl/multimap/

knoxgon
  • 1,070
  • 2
  • 15
  • 31
Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
  • Good link on the both the difference and how it works internally. [link](http://www.ccplusplus.com/2014/02/how-map-and-multimap-works-c.html) – Rndp13 May 29 '15 at 05:46
10

The latter requires that the values can be ordered (either via operator< or a comparison-function), the former does not.

Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • It would appear operator< works the same on either map or multimap? http://en.cppreference.com/w/cpp/container/map/operator_cmp – johnbakers Jun 24 '13 at 02:32
  • Yes, but my answer referred to the ordering of the values. Suppose you have a type `T` that as no ordering. You can use it to create an `std::multimap`, but you cannot use to the create an `std::map >`. – Björn Pollex Jun 24 '13 at 12:49