C++ multimap - emplace() Function
The C++ multimap::emplace function is used to insert a new element in the multimap. The insertion of the new element increases the size of the multimap by one. As a multimap is an ordered data container, hence it stores the new element in its respective position to keep the multimap sorted.
Syntax
template <class... Args> iterator emplace (Args&&... args);
Parameters
args |
Arguments forwarded to construct the new element of the mapped type. |
Return Value
Returns an iterator pointed to newly added element.
Time Complexity
Logarithmic i.e, Θ(log(n)).
Example:
In the example below, the multimap::emplace function is used to insert a new element in the multimap called MyMMap.
#include <iostream> #include <map> using namespace std; int main (){ multimap<string, string> MyMMap; multimap<string, string>::iterator it; MyMMap.insert(pair<string, string>("USA", "New York")); MyMMap.insert(pair<string, string>("USA", "Washington")); MyMMap.insert(pair<string, string>("CAN", "Toronto")); //insert a new element in the MyMMap using emplace function MyMMap.emplace("IND", "Delhi"); MyMMap.emplace("CAN", "Montreal"); cout<<"MyMMap contains:"<<"\n "; for(it = MyMMap.begin(); it != MyMMap.end(); ++it) cout<<it->first<<" => "<<it->second<<"\n "; return 0; }
The output of the above code will be:
MyMMap contains: CAN => Toronto CAN => Montreal IND => Delhi USA => New York USA => Washington
❮ C++ <map> Library