Purpose of Multimap

I understand what a map is and all, but what purpose does multimap serve?

How can you access a specific member if the keys are the same?
multimap used when you need to map different values to the same key.
You can select range containing equal keys using equal_range
http://en.cppreference.com/w/cpp/container/multimap/equal_range
Multimaps allow for multiple entries with the same key.

Here's a quick example to compare map and multimap funcitonality.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <map>

int main()
{
    std::map<std::string, int> m;

    m.insert(std::pair<std::string, int>("a", 1)); //insert one item with key "a"
    m.insert(std::pair<std::string, int>("a", 2)); //insert a second item with key "a"

    for(auto &i: m)
        cout << i.first << " " << i.second << endl;

    return 0;
}
a 1


Regular maps will discard any any subsequent values inserted that have the same key. On the other hand, if you do the same thing with std::multimap<std::string, int> m instead, the output would be:
a 1
a 2

Multimaps allow any amount of values with the same key.

To get all values of key 'a', multimap provides the function equal_range (http://www.cplusplus.com/reference/map/multimap/equal_range/)
that will give you a pair of iterators over the range of the specified key.

Consider the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <map>

int main()
{
    //typedefs to reduce typing.
    typedef std::multimap<std::string, int> mmap;
    typedef std::pair<std::string, int> mmap_pair;

    mmap m;
    m.insert(mmap_pair("a", 1)); //insert a bunch of values
    m.insert(mmap_pair("a", 2));
    m.insert(mmap_pair("a", 3));
    m.insert(mmap_pair("b", 4));
    m.insert(mmap_pair("b", 5));
    m.insert(mmap_pair("c", 6));

    std::pair<mmap::iterator, mmap::iterator> range;
    range = m.equal_range("a"); //equal_range returns pair of iterators

    //loop through the iterator pair like regular iterators
    for(mmap::iterator it = range.first; it != range.second; ++it)
        std::cout << it->first << " " << it->second << endl;

    return 0;
}
a 1
a 2
a 3


So you can do some cool things with multimaps.
That said, it's one of those containers you rarely need to use, but when you do need it, you really need it.
Alright thanks!

I am going to sleep for now but if any questions do pop into my mind I will definitely ask em xD


Thanks again!
Also I assume this the reason why there is a multiset?

But how would that work if each value represents its key for multi-set?
I have used multiset to store prime factors of number. As for example factorization of 24 is 2×2×2×3, set cannot hold several 2s, and we should use multiset.
You can use multiset to store any set where single value can appear more than once.
That makes sense I guess, but beyond that their isn't much use right?
but beyond that their isn't much use right?
Actually there is. You will use multiset if you need a set which can hold several of same value. You might not run into these situation often, but there is people which needs it everyday. This can provide some starting push:
http://stackoverflow.com/questions/2524480/give-me-a-practical-use-case-of-multi-set
https://en.wikipedia.org/wiki/Multiset#Applications

And there is even a paper on use of multiset in mathematics and computer sciense"
http://www.maths.soton.ac.uk/emis/journals/NSJOM/Papers/37_2/NSJOM_37_2_073_092.pdf
So like maybe if your counting how many times a number appears in something you could a multiset?

Alright read those and it makes more sense now thanks!
Topic archived. No new replies allowed.