multimap insert() in C++ STL


In this article we will be discussing the working, syntax, and examples of multimap::insert() function in C++ STL.

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitatesto store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.

What is multimap::insert()?

multimap::insert() function is an inbuilt function in C++ STL, which is defined in  header file. insert() is used to insert new values to the multimap container and increases the size of the container by the number of elements inserted.

Unlike a map container that checks the associated key already present then won’t insert the elements but multimap has the feature of associating multiple elements to the same key.

So, whenever we insert an element it goes to its respective position according to its key.

Syntax

multiMap_name.insert({key& k, value_type& val});

or

multiMap_name.insert(iterator& it, {key& k, value_type& val});

or

multiMap_name.insert(iterator& position1, iterator& position2);

or

multimap_name.insert(initializer_list <value_type> il);

Parameter

This function accepts the following parameter −

  • k − This is the key that is associated with the element. The function checks if the key is already in the container then it doesn’t insert the element.

  • val − The value which is to be inserted.

  • it − The iterator type of value which is used to give the position where we want the element to be inserted.

  • position1, position2 − The position1 is the starting position and position2 is the ending position when we want to insert the series of elements, we can use the range of multiple elements which we want to insert.

  • il − This is the initializer list containing the elements which we are wishing to initialise to the container.

Return value

This function returns an iterator to the element newly inserted in the map container.

Input 

mutlimap<int, char> mymap;
mymap.insert(1, ‘a’);
mymap.insert(2, ‘b’);

Output 

1: a
2: b

Example

//inserting at a given position with a given key

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 4, 40 });
   mul.insert({ 5, 50 });
   //displaying multimap elements
   cout << "Elements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto it = mul.begin(); it!= mul.end(); ++it){
      cout << it->first << '\t' << it->second << '\n';
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

Elements in multimap is :
KEY ELEMENT
1 10
2 20
3 30
4 40
5 50

Example

 Live Demo

//inserting the element after the given key
#include <bits/stdc++.h>
using namespace std;
int main(){
   multimap<int, int> mul;
   //inserting elements in multimap
   mul.insert({ 1, 10 });
   mul.insert({ 2, 20 });
   mul.insert({ 3, 30 });
   mul.insert({ 5, 40 });
   mul.insert({ 6, 50 });
   //finding the element after which we will insert
   auto i = mul.find(3);
   mul.insert(i, { 4, 90 });
   // print the elements
   cout << "KEY\tELEMENT\n";
   for (auto itr = mul.begin(); itr!= mul.end(); ++itr){
      cout << itr->first << '\t' << itr->second << '\n';
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

KEY ELEMENT
1 10
2 20
3 30
4 90
5 40
6 50

Updated on: 22-Apr-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements