C++ Standard Library C++ STL Library

C++ unordered_map - insert() Function



The C++ unordered_map::insert function is used to insert new elements in the container. This results into increasing the unordered_map size by the number of elements inserted. As the element's key in a unordered_map are unique, therefore the insertion operation first checks if the inserted element's key is unique to the map then the element is inserted.

Syntax

//single element version 
pair<iterator,bool> 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

The single element version returns a pair, with pair::first unordered_map to an iterator pointing to either newly inserted element or to element with the equivalent key in the unordered_map. The pair::second is unordered_map to true if new element is inserted in the unordered_map, false otherwise.

The single element with hint version returns an iterator pointing to either newly inserted element or to the equivalent key present in the unordered_map.

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 example below, the unordered_map::insert function is used to insert elements in the given unordered_map.

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

  //populating unordered_map
  uMap[101] = "John";
  uMap[102] = "Marry";
  uMap[103] = "Kim";

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

  //insert single element in the map 
  uMap.insert(pair<int, string>(104, "Jo"));

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

  //insert single element with hint in the map
  it = uMap.begin();
  uMap.insert(it, pair<int, string>(105, "Ramesh"));

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

  return 0;
}

The output of the above code will be:

uMap contains: 
 103  Kim
 102  Marry
 101  John
 
uMap contains: 
 104  Jo
 103  Kim
 102  Marry
 101  John
 
uMap contains: 
 105  Ramesh
 104  Jo
 103  Kim
 102  Marry
 101  John

Example:

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

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

  //populating uMap1
  uMap1[101] = "John";
  uMap1[102] = "Marry";
  uMap1[103] = "Kim";

  //populating uMap2
  uMap2[104] = "Jo";
  uMap2[105] = "Ramesh";

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

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

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

  return 0;
}

The output of the above code will be:

uMap1 contains: 
 103  Kim
 102  Marry
 101  John
 
uMap1 contains: 
 104  Jo
 105  Ramesh
 103  Kim
 102  Marry
 101  John

Example:

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

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

  //populating map
  uMap[101] = "John";
  uMap[102] = "Marry";
  uMap[103] = "Kim";

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

  //insert elements of initializer list into map 
  initializer_list<pair<const int, string>> ilist = 
    {{104,"Jo"}, {105, "Ramesh"}};
  uMap.insert(ilist); 

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

  return 0;
}

The output of the above code will be:

uMap contains: 
 103  Kim
 102  Marry
 101  John
 
uMap contains: 
 105  Ramesh
 104  Jo
 103  Kim
 102  Marry
 101  John

❮ C++ <unordered_map> Library