Multimap in C++ Standard Template Library (STL)
Multimap is similar to map with an addition that multiple elements can have same keys. Rather than each element being unique, the key value and mapped value pair has to be unique in this case. multimap和map相似,多个元素可以有相同的key,不需要每个元素都唯一,但是key和value对必须唯一。key是有序排列的。
Some Basic Functions associated with multimap:
- begin() – Returns an iterator to the first element in the multimap
- end() – Returns an iterator to the theoretical element that follows last element in the multimap
- size() – Returns the number of elements in the multimap
- max_size() – Returns the maximum number of elements that the multimap can hold
- empty() – Returns whether the multimap is empty
- pair<int,int> insert(keyvalue,multimapvalue) – Adds a new element to the multimap
C++ implementation to illustrate above functions
#include <iostream>
#include <map>
#include <iterator>
using namespace std;
int main()
{
multimap <int, int> gquiz1; // empty multimap container
// insert elements in random order
gquiz1.insert(pair <int, int> (1, 40));
gquiz1.insert(pair <int, int> (2, 30));
gquiz1.insert(pair <int, int> (3, 60));
gquiz1.insert(pair <int, int> (4, 20));
gquiz1.insert(pair <int, int> (5, 50));
gquiz1.insert(pair <int, int> (6, 50));
gquiz1.insert(pair <int, int> (6, 10));
// printing multimap gquiz1
multimap <int, int> :: iterator itr;
cout << "\nThe multimap gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// assigning the elements from gquiz1 to gquiz2
multimap <int, int> gquiz2(gquiz1.begin(),gquiz1.end());
// print all elements of the multimap gquiz2
cout << "\nThe multimap gquiz2 after assign from gquiz1 is : \n";
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
// remove all elements up to element with value 30 in gquiz2
cout << "\ngquiz2 after removal of elements less than key=3 : \n";
cout << "\tKEY\tELEMENT\n";
gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
// remove all elements with key = 4
int num;
num = gquiz2.erase(4);
cout << "\ngquiz2.erase(4) : ";
cout << num << " removed \n" ;
cout << "\tKEY\tELEMENT\n";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
//lower bound and upper bound for multimap gquiz1 key = 5
cout << "gquiz1.lower_bound(5) : " << "\tKEY = ";
cout << gquiz1.lower_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second << endl;
cout << "gquiz1.upper_bound(5) : " << "\tKEY = ";
cout << gquiz1.upper_bound(5)->first << '\t';
cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second << endl;
return 0;
}
Output:
The multimap gquiz1 is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50 6 50 6 10 The multimap gquiz2 after assign from gquiz1 is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50 6 50 6 10 gquiz2 after removal of elements less than key=3 : KEY ELEMENT 3 60 4 20 5 50 6 50 6 10 gquiz2.erase(4) : 1 removed KEY ELEMENT 3 60 5 50 6 50 6 10 gquiz1.lower_bound(5) : KEY = 5 ELEMENT = 50 gquiz1.upper_bound(5) : KEY = 6 ELEMENT = 50
List of Functions of Multimap:
- multimap::operator= in C++ STL– It is used to assign new contents to the container by replacing the existing contents.
- multimap::crbegin() and multimap::crend() in C++ STL– crbegin() returns a constant reverse iterator referring to the last element in the multimap container. crend() returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap.
- multimap::emplace_hint() in C++ STL– Inserts the key and its element in the multimap container with a given hint.
- multimap clear() function in C++ STL– Removes all the elements from the multimap.
- multimap empty() function in C++ STL– Returns whether the multimap is empty.
- multimap maxsize() in C++ STL– Returns the maximum number of elements a multimap container can hold.
- multimap value_comp() function in C++ STL– Returns the object that determines how the elements in the multimap are ordered (‘<‘ by default)
- multimap rend in C++ STL– Returns a reverse iterator pointing to the theoretical element preceding to the first element of the multimap container.
- multimap::cbegin() and multimap::cend() in C++ STL– cbegin() returns a constant iterator referring to the first element in the multimap container. cend() returns a constant iterator pointing to the theoretical element that follows last element in the multimap.
- multimap::swap() in C++ STL– Swap the contents of one multimap with another multimap of same type and size.
- multimap rbegin in C++ STL– Returns an iterator pointing to the last element of the container.
- multimap size() function in C++ STL– Returns the number of elements in the multimap container.
- multimap::emplace() in C++ STL– Inserts the key and its element in the multimap container.
- multimap::begin() and multimap::end() in C++ STL– begin() returns an iterator referring to the first element in the multimap container. end() returns an iterator to the theoretical element that follows last element in the multimap.
- multimap upper_bound() function in C++ STL– Returns an iterator to the first element that is equivalent to multimapped value with key value ‘g’ or definitely will go after the element with key value ‘g’ in the multimap.
- multimap::count() in C++ STL– Returns the number of matches to element with key value ‘g’ in the multimap.
- multimap::erase() in C++ STL– Removes the key value from the multimap.
- multimap::find() in C++ STL– Returns an iterator to the element with key value ‘g’ in the multimap if found, else returns the iterator to end.
- multimap equal_range() in C++ STL– Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k.
- multimap insert() in C++ STL– Used to insert elements in the multimap container.
- multimap lower_bound() function in C++ STL– Returns an iterator to the first element that is equivalent to multimapped value with key value ‘g’ or definitely will not go before the element with key value ‘g’ in the multimap.
- multimap key_comp() in C++ STL– Returns the object that determines how the elements in the multimap are ordered (‘<‘ by default).
multimap insert() in C++ STL
The multimap::insert is a built-in function in C++ STL which is used to insert elements in the multimap container.
- Syntax:
iterator multimap_name.insert({key, element})
Parameters: The function accepts a pair that consists of a key and element which is to be inserted into the multimap container.
Return Value: The function returns an iterator pointing to the new element in the container.
// C++ program to illustrate
// multimap::insert({key, element})
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
// prints the elements
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
KEY ELEMENT 1 40 2 30 2 20 3 60 5 50
- Syntax:
iterator multimap_name.insert(iterator position, {key, element})
Parameters: The function accepts two parameters which is described below:
Return Value: The function returns an iterator pointing to the new element in the container.
- {key, element}: this specifies a pair that consists of a key and element which is to be inserted into the multimap container.
- position: this does not specify the position where the insertion is to be done, it only points a position from where the searching operation for insertion is to be started. The insertion is done according to the order which is followed by the multimap container.
// C++ program to illustrate
// multimap::insert({key, element})
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
auto it = mp.find(2);
// inserts {3, 6} starting the search from
// position where 2 is present
mp.insert(it, { 3, 60 });
// prints the elements
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
KEY ELEMENT 1 40 2 30 3 60
- Syntax:
iterator multimap_name.insert(iterator position1, iterator position2)
Parameters: The function accepts two parameters position1 and position2 which specifies the range of elements. All the elements in the range [position1, last) are inserted in the multimap container.
Return Value: The function returns an iterator pointing to the new element in the container.
// C++ program to illustrate
// multimap::insert({key, element})
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp, mp1;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
// inserts all elements in range [begin, end)
// in mp1
mp1.insert(mp.begin(), mp.end());
// prints the elements
cout << "Elements in mp1 are\n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp1.begin(); itr != mp1.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
Elements in mp1 are KEY ELEMENT 1 40 2 30
multimap find() in C++ STL
multimap::find() is a built-in function in C++ STL which returns an iterator or a constant iterator that refers to the position where the key is present in the multimap. If the key is not present in the multimap container, it returns an iterator or a constant iterator which refers to multimap.end(). 返回找到的元素的迭代器,没找到返回最后一个元素的迭代器
Syntax:
iterator multimap_name.find(key) or constant iterator multimap_name.find(key)
Parameters: The function accepts one mandatory parameter key which specifies the key to be searched in the multimap container.
Return Value: The function returns an iterator or a constant iterator which refers to the position where the key is present in the multimap. If the key is not present in the multimap container, it returns an iterator or a constant iterator which refers to multimap.end().
// C++ program for illustration
// of multimap::find() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 2, 60 });
mp.insert({ 3, 20 });
mp.insert({ 1, 50 });
mp.insert({ 4, 50 });
cout << "The elements from position 3 in multimap are : \n";
cout << "KEY\tELEMENT\n";
// find() function finds the position at which 3 is
for (auto itr = mp.find(3); itr != mp.end(); itr++)
cout << itr->first
<< '\t' << itr->second << '\n';
return 0;
}
Output:
The elements from position 3 in multimap are : KEY ELEMENT 3 20 4 50
multimap::erase() in C++ STL
multimap::erase() is a built-in function in C++ STL which is used to erase element from the container. It can be used to erase keys, elements at any specified position or a given range. 删除特定元素,获得特定范围的元素。
- Syntax for erasing a key:
multimap_name.erase(key)
Parameters: The function accepts one mandatory parameter key which specifies the key to be erased in the multimap container.
Return Value: The function does not return anything. It erases all the elements with the specified key.
// C++ program to illustrate
// multiset::erase(key)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
// prints the elements
cout << "The multimap before using erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
// function to erase given keys
mp.erase(1);
mp.erase(2);
// prints the elements
cout << "\nThe multimap after applying erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 3 60
- Syntax for removing a position:
multimap_name.erase(iterator position)
Parameters: The function accept one mandatory parameter position which specifies the iterator that is the reference to the position of the element to be erased.
Return Value: The function does not returns anything.
Program below illustrate the above syntax:
// C++ program to illustrate
// multiset::erase(position)
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
// prints the elements
cout << "The multimap before using erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
// function to erase given position
auto it = mp.find(2);
mp.erase(it);
auto it1 = mp.find(5);
mp.erase(it1);
// prints the elements
cout << "\nThe multimap after applying erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 3 60 2 20 1 40
- Syntax for erasing a given range:
multimap_name.erase(iterator position1, iterator position2)
Parameters: The function accepts two mandatory parameters which are described below:
Return Value: The function does not returns anything. It removes all the elements in the given range of iterators.
Program below illustrate the above syntax:
- position1 – specifies the iterator that is the reference to the element from which removal is to be done.
- position2 – specifies the iterator that is the reference to the element upto which removal is to be done.
// C++ program to illustrate
// multiset::erase()
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 2, 20 });
mp.insert({ 5, 50 });
// prints the elements
cout << "The multimap before using erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
// function to erase in a given range
// find() returns the iterator reference to
// the position where the element is
auto it1 = mp.find(2);
auto it2 = mp.find(5);
mp.erase(it1, it2);
// prints the elements
cout << "\nThe multimap after applying erase() is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.crbegin(); itr != mp.crend(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
The multimap before using erase() is : KEY ELEMENT 1 40 2 30 2 20 3 60 5 50 The multimap after applying erase() is : KEY ELEMENT 5 50 1 40
multimap::count() in C++ STL
The multimap::count is a built-in function in C++ STL which returns the number of times a key is present in the multimap container. 返回key的元素的个数。
Syntax:
multimap_name.count(key)
Parameters: The function accepts one mandatory parameter key which specifies the key whose count in multimap container is to be returned.
Return Value: The function returns the number of times a key is present in the multimap container.
// C++ function for illustration
// multiset::count() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 2, 60 });
mp.insert({ 2, 20 });
mp.insert({ 1, 50 });
mp.insert({ 4, 50 });
// count the number of times
// 1 is there in the multimap
cout << "1 exists " << mp.count(1)
<< " times in the multimap\n";
// count the number of times
// 2 is there in the multimap
cout << "2 exists " << mp.count(2)
<< " times in the multimap\n";
return 0;
}
Output:
1 exists 2 times in the multimap 2 exists 3 times in the multimap
multimap::begin() and multimap::end() in C++ STL
- multimap::begin() is a built-in function in C++ STL which returns an iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container’s sorting criterion.
Syntax:
multimap_name.begin()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
// C++ function to illustrate
// the multimap::begin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
auto ite = mp.begin();
cout << "The first element is: ";
cout << "{" << ite->first << ", "
<< ite->second << "}\n";
// prints the elements
cout << "\nThe multimap is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
The first element is: {1, 40} The multimap is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
- multimap::end() is a built-in function in C++ STL which returns an iterator to the theoretical element that follows last element in the multimap. Since multimap container contains the element in an ordered way, end() will point to that theoretical position which follows the last element according to the container’s sorting criterion.
Syntax:
multimap_name.end()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
// C++ function to illustrate
// the multimap::end() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
// prints the elements
cout << "\nThe multimap is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n';
}
return 0;
}
Output:
The multimap is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
multimap size() function in C++ STL
The multimap::size() is a built-in function in C++ STL which returns the number of elements in the multimap container. 返回容器中元素的个数。
Syntax:
multimap_name.size()
Parameters: The function does not accept any parameter.
Return Value: This function returns the number of elements a multimap container has.
// C++ function for illustration
// multimap::size() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 2, 60 });
mp.insert({ 2, 20 });
mp.insert({ 1, 50 });
mp.insert({ 4, 50 });
cout << "multimap mp has " << mp.size()
<< " number of elements";
return 0;
}
Output:
multimap mp has 6 number of elements
multimap clear() function in C++ STL
The multimap clear() function is an inbuilt function in C++ STL which is used to remove all elements from the multimap container (which are destroyed), leaving the container with a size of 0. 删除容器中所有元素,大小变为零。
Syntax :
mymultimap_name.clear()
Parameters: This function does not take any arguments.
Return Value: This function does not returns anything. The return type of the function is void. It just empties the whole container.
Below program illustrate the multimap::clear() function in C++:
// CPP program to illustrate the
// multimap::clear() function
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Creating multimap of string and int
multimap<string, int> mymultimap;
// Inserting 3 Items with their value
// using insert function
mymultimap.insert(pair<string, int>("Item1", 10));
mymultimap.insert(pair<string, int>("Item2", 20));
mymultimap.insert(pair<string, int>("Item3", 30));
cout << "Size of the multimap before using "
<< "clear function : ";
cout << mymultimap.size() << '\n';
// Removing all the elements
// present in the multimap
mymultimap.clear();
cout << "Size of the multimap after using"
<< " clear function : ";
cout << mymultimap.size() << '\n';
return 0;
}
Output:
Size of the multimap before using clear function : 3 Size of the multimap after using clear function : 0
multimap::emplace() in C++ STL
The multimap::emplace() is a built-in function in C++ STL which inserts the key and its element in the multimap container. It effectively increases the container size by one as multimap is the container that stores multiple keys with same values.
Syntax:
multimap_name.emplace(key, element)
Parameters: The function accepts two mandatory parameters which are described below:
- key – specifies the key to be inserted in the multimap container.
- element – specifies the element to the key which is to be inserted in the multimap container.
Return Value: The function does not return anything.
// C++ program for the illustration of
// multiset::emplace() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialize container
multimap<int, int> mp;
// insert elements in random order
mp.emplace(2, 30);
mp.emplace(1, 40);
mp.emplace(2, 60);
mp.emplace(2, 20);
mp.emplace(1, 50);
mp.emplace(4, 50);
// prints the elements
cout << "\nThe multimap is : \n";
cout << "KEY\tELEMENT\n";
for (auto itr = mp.begin(); itr != mp.end(); itr++)
cout << itr->first << "\t" << itr->second << endl;
return 0;
}
Output:
The multimap is : KEY ELEMENT 1 40 1 50 2 30 2 60 2 20 4 50
multimap empty() function in C++ STL
The multimap::empty() is a boolean type observer function in C++ STL which tells whether the container is empty or not. This function returns true when the multimap container is empty (i.e. the size of the container is 0). Being an observer function it does not modify the multimap in any way. 判断容器是否为空。
Syntax:
multimap1.empty()
Return Value: This method returns a boolean type value. It returns true when multimap is empty else returns false.
Below program illustrate the multimap::empty() function:
// C++ program to demonstrate
// std::multimap::empty
#include <iostream>
#include <map>
using namespace std;
int main()
{
// declaring multimap
multimap<char, int> mmap;
// checking if mmap is empty or not
if (mmap.empty())
cout << "multimap is empty\n";
// inserting values to mmap
// thus making it non empty
mmap.insert(pair<char, int>('a', 26));
mmap.insert(pair<char, int>('b', 30));
mmap.insert(pair<char, int>('c', 44));
// checking that mmap is now not empty
if (mmap.empty())
cout << "multimap is empty\n";
else
cout << "multimap is not empty\n";
return 0;
}
Output:
multimap is empty multimap is not empty