Summary: The Multimap container for the standard library is very similar to the Map association container-but MULTIMAP allows duplicate keys. This feature makes Multimap more useful than you might think. This article will discuss it.
In the article "Using <map> Library to create an associative container," We discussed the Map Association container in the standard library. But that's just part of the map container. The standard library also defines a Multimap container, which is similar to a map, but it allows duplicate keys. This attribute makes Multimap more useful than expected: for example, the same person in the phone book can have more than two phone numbers, the file system can map multiple symbolic links to the same physical file, or the DNS server can map several URLs to the same IP address. On these occasions, you can look like this:
// 注: 伪码
multimap <string, string> phonebook;
phonebook.insert("Harry","8225687"); // 家里电话
phonebook.insert("Harry","555123123"); // 单位电话
phonebook.insert("Harry"," 2532532532"); // 移动电话
The ability to store duplicate keys in Multimap greatly affects its interface and use. So how do you create an associative container for a non unique key? The answer is to use the Multimap container defined in the <map> library.
Ask a question
Unlike map, Multimap can contain duplicate keys. This poses a problem: how do overloaded subscript operators return multiple association values for the same key? Take the following pseudo code as an example:
string phone=phonebook["Harry];
The solution to the problem of the designer of the standard library is to drop the label operator from the Multimap. Therefore, there are different ways to insert and get elements, and to do error handling.
Insert
Suppose you need to develop a DNS daemon (that is, a service program in a Windows system) that maps IP addresses to matching URL strings. You know that in some cases the same IP address is to be associated with multiple URLs. These URLs all point to the same site. In this case, you should use Multimap instead of map. For example:
#include <map>
#include <string>
multimap <string, string> DNS_daemon;
Inserts an element with the insert () member function instead of the subscript operator. Insert () has a parameter of type pair. In the use <map> Library to create an association container we demonstrated how to use the Make_pair () helper function to accomplish this task. You can also use it as follows:
DNS_daemon.insert (make_pair("213.108.96.7","cppzone.com"));
In the Insert () call above, the string "213.108.96.7" is the key, and "cppzone.com" is the value associated with it. The same keys are inserted at a later time, with different associated values:
DNS_daemon.insert (make_pair("213.108.96.7","cppluspluszone.com"));
Therefore, Dns_daemon contains two elements with the same key value. Note that the values returned by Multimap::insert () and Map::insert () are different.
typedef pair <const Key, T> value_type;
iterator
insert(const value_type&); // #1 multimap
pair <iterator, bool>
insert(const value_type&); // #2 map
The Multimap::insert () member function returns the iteration pointer to the newly inserted element, that is, iterator (Multimap::insert () always succeeds). But Map::insert () returns Pair<iterator, Bool>, where the bool value indicates whether the insert operation was successful.