C++ Standard Library C++ STL Library

C++ multimap - insert() Function



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

Syntax

//single element version  
iterator 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 
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 multimap.

Time Complexity

  • Logarithmic i.e, Θ(log(n)) if a single element is inserted, but constant i.e, Θ(1) if position provided is optimal.
  • Multiple elements insertion: Linearithmic: number of elements inserted multiplied by log of (container size + number of elements inserted). But, in C++ 98, it is linear in (container size + number of elements inserted), if the elements are already sorted.

Example:

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

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

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

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

  //insert single element in the multimap 
  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 ";

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

  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: 
 CAN  Montreal
 IND  Mumbai
 USA  New York
 
MMap contains: 
 CAN  Montreal
 IND  Mumbai
 IND  Delhi
 USA  New York
 
MMap contains: 
 CAN  Montreal
 IND  Mumbai
 IND  Delhi
 USA  Washington
 USA  New York

Example:

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

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

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

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

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

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

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

  return 0;
}

The output of the above code will be:

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

Example:

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

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

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

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

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

  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: 
 CAN  Montreal
 IND  Mumbai
 USA  New York
 
MMap contains: 
 CAN  Montreal
 IND  Mumbai
 IND  Delhi
 USA  New York
 USA  Washington

❮ C++ <map> Library