Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ unordered_multimap::insert function is used to insert new elements in the container. This results into increasing the unordered_multimap size by the number of elements inserted.

Syntax

//single element version 
iterator insert (const value_type& val);
template <class P> pair<iterator,bool> insert (P&& val);

//single element with hint version   
iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

//initializer list version 
void insert (initializer_list<value_type> il);

Parameters

position Specify the hint for the position where the element can be inserted.
val Specify the value to be copied (or moved) to the inserted elements.
first Specify the starting position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
last Specify the last position of InputIterator. Copies of the elements in the range [first,last) are inserted in the container.
il Specify the initializer_list object.

Return Value

Returns an iterator pointing to newly inserted element in the unordered_multimap.

Time Complexity

  • Single element insertion: Average case - constant i.e, Θ(1). Worst case - linear i.e, Θ(n).
  • Multiple elements insertion: Average case - linear in number of elements inserted. Worst case - quadratic: number of elements inserted multiplied by (container size + 1).

Example:

In the below example, the unordered_multimap::insert function is used to insert elements in the given unordered_multimap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  unordered_multimap<string, string>::iterator it;

  //populating multimap
  uMMap.insert(pair<string, string>("CAN", "Montreal"));
  uMMap.insert(pair<string, string>("IND", "Mumbai"));
  uMMap.insert(pair<string, string>("USA", "New York"));

  cout<<"uMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  //insert single element in the multimap 
  uMMap.insert(pair<string, string>("IND", "Delhi"));

  cout<<"\nuMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  //insert single element with hint in the multimap
  it = uMMap.begin();
  uMMap.insert(it, pair<string, string>("USA", "Washington"));

  cout<<"\nuMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

uMMap contains: 
 USA  New York
 CAN  Montreal
 IND  Mumbai
 
uMMap contains: 
 USA  New York
 CAN  Montreal
 IND  Delhi
 IND  Mumbai
 
uMMap contains: 
 USA  New York
 USA  Washington
 CAN  Montreal
 IND  Delhi
 IND  Mumbai

Example:

A range of elements can also be inserted into a unordered_multimap. Consider the below example.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap1;
  unordered_multimap<string, string> uMMap2;
  unordered_multimap<string, string>::iterator it;

  //populating uMMap1
  uMMap1.insert(pair<string, string>("CAN", "Montreal"));
  uMMap1.insert(pair<string, string>("IND", "Mumbai"));
  uMMap1.insert(pair<string, string>("USA", "New York"));

  //populating uMMap2
  uMMap2.insert(pair<string, string>("IND", "Delhi"));
  uMMap2.insert(pair<string, string>("USA", "Washington"));

  cout<<"uMMap1 contains: \n ";
  for(it = uMMap1.begin(); it != uMMap1.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  //inserts a range of elements from uMMap2 to uMMap1 
  uMMap1.insert(uMMap2.begin(), uMMap2.end());

  cout<<"\nuMMap1 contains: \n ";
  for(it = uMMap1.begin(); it != uMMap1.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

uMMap1 contains: 
 USA  New York
 CAN  Montreal
 IND  Mumbai
 
uMMap1 contains: 
 USA  Washington
 USA  New York
 CAN  Montreal
 IND  Delhi
 IND  Mumbai

Example:

Similarly, the initializer list version can be used to insert elements in the given unordered_multimap.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap;
  unordered_multimap<string, string>::iterator it;

  //populating uMMap
  uMMap.insert(pair<string, string>("CAN", "Montreal"));
  uMMap.insert(pair<string, string>("IND", "Mumbai"));
  uMMap.insert(pair<string, string>("USA", "New York"));

  cout<<"uMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  //insert elements of initializer list into multimap 
  initializer_list<pair<string const, string>> ilist = 
      {{"IND", "Delhi"}, {"USA", "Washington"}};
  uMMap.insert(ilist); 

  cout<<"\nuMMap contains: \n ";
  for(it = uMMap.begin(); it != uMMap.end(); ++it)
     cout<<it->first<<"  "<<it->second<<"\n ";

  return 0;
}

The output of the above code will be:

uMMap contains: 
 USA  New York
 CAN  Montreal
 IND  Mumbai
 
uMMap contains: 
 USA  Washington
 USA  New York
 CAN  Montreal
 IND  Delhi
 IND  Mumbai

❮ C++ - Unordered Multimap