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


The C++ map::insert function is used to insert new elements in the container. This results into increasing the map size by the number of elements inserted. As the element's key in a 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);

//single element with hint version  
iterator insert (iterator position, const value_type& val);

//range version 
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);
//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 map to an iterator pointing to either newly inserted element or to element with the equivalent key in the map. The pair::second is map to true if new element is inserted in the 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 map.

Time Complexity

Logarithmic i.e, Θ(log(n)) if a single element is inserted, but constant i.e, Θ(1) if position provided is optimal.

Example:

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

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

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

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

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

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

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

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

  return 0;
}

The output of the above code will be:

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

Example:

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

#include <iostream>
#include <map>
using namespace std;
 
int main (){
  map<int, string> Map1;
  map<int, string> Map2;
  map<int, string>::iterator it;

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

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

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

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

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

  return 0;
}

The output of the above code will be:

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

Example:

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

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

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

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

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

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

  return 0;
}

The output of the above code will be:

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

❮ C++ - Map