Updated March 20, 2023
Introduction to Multimap in C++
Multimap in C++ programming language is like an associate container which is quite similar to the map. This container contains a sorted-list of key-value pairs while permitting multiple elements with the same key. The main difference between map and multi-map is that when you are using a multi-map feature in your code then you can have the same keys for a set of multiple elements. Or we can say having multiple entries in a map with the same keys. Unlike the map, a multi-map can have duplicate keys associated with the same elements because having duplicate keys for the same elements is not allowed on the map.
Let’s have a look at the syntax of a multi-map. Suppose you want to create multi-map consists of integer and character then this is how you will define it:
Syntax:
template <class Key,
class Type,
class Traits=less <Key>,
class Allocator=allocator <pair <const Key, Type>>>
class multimap;
Parameters of Multimap in C++
Now let’s discuss the parameters in multi-map used in the C++ programming language. From the above syntax, you can see the parameters we have used to define a multi-map.
1. Key
As every element in the map is identified using a key value. The key can be of different types. The data type of key is to be stored in a multi-map container.
multimap::key_type
2. Type
It’s different from the key as a data type of element will be stored in a multi-map container. Each multi-map element will store some data as a mapped value.
multimap::mapped_type
3. Traits
We can use compare keyword instead of traits as both serve the same functionality. As it takes two parameter keys as an argument and returns a boolean value because it’s like a binary predictor. To compare two elements values it provides a function object.
multimap::key_compare
4. Allocator
It represents the object stored in the allocator which is used to define the storage allocation model. The Allocator class we use is the simplest memory allocation and it is also value-independent.
multimap::allocator_type
Member Functions of Multimap in C++
As we have seen the parameters of multi-map. Now it’s time to understand the member functions in multi-map:
Member Functions | Definition |
Constructor | This will construct the multi-map that is empty. |
Destructor | This will destroy the created multi-map. |
Operator | This will assign the values to the container. |
get_allocator | This will return the associated allocator. |
Member Functions with Iterators
Member Functions(iterators) | Definition |
begin | This will returns an iterator addressing the first multi-map element. |
cbegin | This will returns a constant iterator addressing the first multi-map element. |
end | This will returns an iterator addressing the last multi-map element. |
cend | This will returns a constant iterator addressing the last multi-map element. |
rbegin | This will returns a reverse iterator addressing the multi-map beginning. |
crbegin | This will returns a constant reverse iterator addressing the multi-map beginning. |
rend | This will returns a reverse iterator addressing the multi-map ending. |
crend | This will returns a constant iterator addressing the multi-map ending. |
Member Functions with Modifiers
Member Functions(modifiers) | Definition |
clear | This will erase all the multi-map elements. |
insert | This will insert elements in the multi-map. |
emplace | This will construct and insert an element at a particular place in the multi-map. |
emplace_hint | This will construct and insert an element at a particular place in the multi-map with a hint. |
erase | This will erase the multi-map elements. |
swap | This will swap the multi-map elements. |
extract | This will extract the nodes from a multi-map container. |
merge | This will merge nodes from a container in the multi-map. |
Member Functions with Lookup
Member Functions(lookup) | Definition |
count | This will count and returns the number of elements matching the specific key. |
find | This will find the elements with a specific key. |
contains | This will check in a container for elements with a specific key. |
equal_range | This will return a range of elements matching a given key. |
lower_bound | This will return an iterator to the first element no less than a given key. |
upper_bound | This will return an iterator to the first element greater than a given key. |
Member Functions with Capacity
Member Functions(capacity) | Definition |
empty | This will check if the container is empty. |
size | This will returns the number of elements in a multi-map. |
max_size | This will returns the maximum possible number of elements in a multi-map. |
Examples of Multimap in C++
Now let’s see some C++ programming examples to understand the multi-map properly:
Example #1
Code:
#include <iostream>
#include <map>
struct Dot { double i, j; };
struct DotCompare {
bool operator()(const Dot& lhs, const Dot& rhs) const {
return lhs.i < rhs.i; // NB. ignores y on purpose
}
};
int main() {
std::multimap<int, int> n = {{1,1},{2,2},{3,3}};
for(auto& p: n) std::cout << p.first << ' ' << p.second << '\n';
// comparison
std::multimap<Dot, double, DotCompare> mag{
{ {5, 12}, 13 },
{ {3, 4}, 5 },
{ {8, 15}, 17 },
};
for(auto p : mag)
std::cout << "The magnitude of (" << p.first.i
<< ", " << p.first.j << ") is "
<< p.second << '\n';
}
Output:
Example #2
Here is another C++ code implementing the begin member function.
Code:
#include <iostream>
#include <map>
int main ()
{
std::multimap<char,int> food,chocobar; // defining multi-map
food.insert (std::make_pair('p',20));
food.insert (std::make_pair('q',45));
chocobar.insert (std::make_pair('y',128));
chocobar.insert (std::make_pair('y',178));
// food ({{p,20},{q,45}}) vs chocobar ({y,128},{y,178}}):
if (food==chocobar) std::cout << "food and chocobar are equal\n";
if (food!=chocobar) std::cout << "food and chocobar are not equal\n";
if (food< chocobar) std::cout << "food is less than chocobar\n";
if (food> chocobar) std::cout << "food is greater than chocobar\n";
if (food<=chocobar) std::cout << "food is less than or equal to chocobar\n";
if (food>=chocobar) std::cout << "food is greater than or equal to chocochocobar\n";
return 0;
}
Output:
Conclusion
The multi-map help coders in saving a huge amount of time. Suppose you have a collection of items that have the same key-value now you want to run a search for finding some value. Then it’s better to use an iterator and run through multi-map instead of vectors & maps.
Recommended Articles
This is a guide to Multimap in C++. Here we discuss the syntax, Parameters, and member functions of multimap in C++ along with examples and code implementation. You may also look at the following articles to learn more-