C++ multimap - multimap() Function
The C++ multimap::multimap function is used to construct a multimap object, initializing its contents depending on the version of constructor used:
Syntax
//default version - construct an empty //container with no elements explicit multimap (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> multimap (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); //copy version - copies all elements //of x into the container multimap (const multimap& x);
//default version - construct an empty //container with no elements explicit multimap (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); explicit multimap (const allocator_type& alloc); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> multimap (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& = allocator_type()); //copy version - copies all elements //of x into the container multimap (const multimap& x); multimap (const multimap& x, const allocator_type& alloc); //move version - moves elements of x //into the container multimap (multimap&& x); multimap (multimap&& x, const allocator_type& alloc); //initializer list version - copies all //elements of il into the container multimap (initializer_list<value_type> il, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type());
//default version - construct an empty //container with no elements multimap(); explicit multimap (const key_compare& comp, const allocator_type& alloc = allocator_type()); explicit multimap (const allocator_type& alloc); //range version - Constructs a container with //elements as the range [first,last) template <class InputIterator> multimap (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& = allocator_type()); template <class InputIterator> multimap (InputIterator first, InputIterator last, const allocator_type& = allocator_type()); //copy version - copies all elements //of x into the container multimap (const multimap& x); multimap (const multimap& x, const allocator_type& alloc); //move version - moves elements of x //into the container multimap (multimap&& x); multimap (multimap&& x, const allocator_type& alloc); //initializer list version - copies all //elements of il into the container multimap (initializer_list<value_type> il, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); multimap (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());
Parameters
alloc |
Specify the Allocator object. The container keeps and uses an internal copy of this allocator. |
comp |
A binary predicate that takes two keys of map as arguments and returns a bool. Keys are sorted by using this function. |
first |
Specify initial position of the input iterator of the range. The range used is [first,last). |
last |
Specify final position of the input iterator of the range. The range used is [first,last). |
x |
Specify a multimap object of same type. |
il |
Specify an initializer_list object. |
Return Value
Constructor never returns value.
Time Complexity
Constant i.e, Θ(1), for default version and move version.
For all the other cases, Linear i.e, Θ(n).
Example: using default version
In the example below, the multimap::multimap function is used to construct a multimap object.
#include <iostream> #include <map> using namespace std; int main (){ //default version - construct an empty multimap multimap<string, string> MMap; multimap<string, string>::iterator it; cout<<"MMap contains:\n"; for(it = MMap.begin(); it != MMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //populating map MMap.insert(pair<string, string>("CAN", "Montreal")); MMap.insert(pair<string, string>("IND", "Delhi")); cout<<"\nMMap contains:\n"; for(it = MMap.begin(); it != MMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
MMap contains: MMap contains: CAN Montreal IND Delhi
Example: using range and copy version
A multimap can also be constructed using range or copy version. Consider the following example:
#include <iostream> #include <map> using namespace std; int main (){ multimap<string, string> MMap1; multimap<string, string>::iterator it; //populating map MMap1.insert(pair<string, string>("CAN", "Montreal")); MMap1.insert(pair<string, string>("IND", "Delhi")); //range version - construct MMap2 using range multimap<string, string> MMap2(MMap1.begin(), MMap1.end()); //copy version - construct MMap3 from MMap1 multimap<string, string> MMap3(MMap1); cout<<"MMap2 contains:\n"; for(it = MMap2.begin(); it != MMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nMMap3 contains:\n"; for(it = MMap3.begin(); it != MMap3.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
MMap2 contains: CAN Montreal IND Delhi MMap3 contains: CAN Montreal IND Delhi
Example: using move version
Using the move version of multimap, the content of one multimap can be moved to another multimap. Consider the following example:
#include <iostream> #include <map> using namespace std; int main (){ multimap<string, string> MMap1; multimap<string, string>::iterator it; //populating map MMap1.insert(pair<string, string>("CAN", "Montreal")); MMap1.insert(pair<string, string>("IND", "Delhi")); cout<<"MMap1 contains:\n"; for(it = MMap1.begin(); it != MMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; //moving all content of MMap1 into MMap2 multimap<string, string> MMap2(move(MMap1)); cout<<"\nMMap1 contains:\n"; for(it = MMap1.begin(); it != MMap1.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; cout<<"\nMMap2 contains:\n"; for(it = MMap2.begin(); it != MMap2.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
MMap1 contains: CAN Montreal IND Delhi MMap1 contains: MMap2 contains: CAN Montreal IND Delhi
Example: using initializer list version
The initializer list can also be used to assign values into a multimap container. Consider the example below:
#include <iostream> #include <map> using namespace std; int main (){ //creating initializer list initializer_list<pair<const string, string>> ilist = {{"CAN", "Montreal"}, {"IND", "Delhi"}}; //initializer list version - copies all //elements of ilist into the container multimap<string, string> MMap(ilist); multimap<string, string>::iterator it; cout<<"MMap contains:\n"; for(it = MMap.begin(); it != MMap.end(); ++it) cout<<it->first<<" "<<it->second<<"\n"; return 0; }
The output of the above code will be:
MMap contains: CAN Montreal IND Delhi
❮ C++ <map> Library