Multimap Insert Function

The C++ multimap insert is used to insert an element or range of elements into a multimap.

Syntax

Example

single element (1)     pair<iterator,bool> insert (const value_type& val);   //until C++ 11

 hint (2)          iterator insert (iterator position, const value_type& val);   //until C++ 11

range (3)	template <class InputIterator>
  		   void insert (InputIterator first, InputIterator last);        //until C++ 11

single element (1)  pair<iterator,bool> insert (const value_type& val);
		  template <class P> pair<iterator,bool> insert (P&& val); //since C++ 11
            
hint (2)	iterator insert (const_iterator position, const value_type& val);
		   template <class P> iterator insert (const_iterator position, P&& val);

range (3)	template <class InputIterator>
  		   void insert (InputIterator first, InputIterator last); //since C++ 11

initializer list (4)	void insert (initializer_list<value_type> il);   //since C++ 11

Parameter

val : Key value to insert in the multimap.

position : Hint for the position to insert element.

first : Beginning of range to insert.

last : End of range to insert.

il : An initializer list.

Return value

If a single element is inserted then it returns an iterator to the position where the new element was inserted into the multimap.

Or if the element is inserted with hint then it returns an iterator thacpp tutorial to the position where the new element was inserted into the multimap.

Complexity

  • If a single element is inserted complexity will be logarithmic in size.
  • If a hint is given and the position given is the optimal then the complexity will be amortized constant.
  • Iterator validity

No changes.

Data Races

The container is modified.

Exception safety

This function does not throw exception.

Example 1

Let's see the simple example to insert the elements into the multimap:

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   multimap<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            };

   // inserting new element
   m.insert(pair<char, int>('b', 4));
   m.insert(pair<char, int>('c', 5));

   cout << "Multimap contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

Output:

Output

Multimap contains following elements
a = 1
b = 2
b = 3
c = 4
c = 5

In the above example, it simply insert the element with the given key value pair.

Example 2

Let's see a simple example to insert the element in the specified position:

Example

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   multimap<char, int> m = {
            {'b', 2},
            {'c', 3},
            {'d', 4},
            };

   //inserting element with the given position
   m.insert(m.begin(), pair<char, int>('a', 1));  
   m.insert(m.end(), pair<char, int>('d', 5));

   cout << "Multimap contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

Output:

Output

Multimap contains following elements
a = 1
b = 2
c = 3
d = 4
d = 5

In the above example, elements are inserted in the defined position i.e. in begin element {'a', 1} is inserted and in the end element {'d', 5} is inserted.

Example 3

Let's see a simple example to insert the elements of one multimap to another:

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   
   multimap<char, int> m1 = {
            {'a', 1},
            {'b', 2},
            {'a', 3},
            {'d', 4},
            {'b', 5},
            };

multimap<char, int> m2;  // creating new multimap m2
m2.insert(m1.begin(), m1.end());   //inserting the elements of m1 to m2 from begin to end

   cout << "Multimap contains following elements" << endl;

   for (auto it = m2.begin(); it != m2.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

Output:

Output

Multimap contains following elements
a = 1
a = 3
b = 2
b = 5
d = 4

In the above example, multimap m1 has five elements and multimap m2 is empty. insert is using to insert the element of m1 to m2 from begin of m1 to end of m1 and displaying the content of m2 multimap.

Example 4

Let's see a simple example to insert the element:

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   multimap<int , string> m = {
            {1, "Java"},
            {2, "C++"},
            {3, "SQL"},
            };

   m.insert({{4,"VB"}, {3, "Oracle"}});

   cout << "Multimap contains following elements" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " : " << it->second << endl;

   return 0;
}

Output:

Output

Multimap contains following elements
1 : Java
2 : C++
3 : SQL
3 : Oracle
4 : VB

In the above example, another form of insert function is used to insert the elements into the multimap.

Input Required

This code uses input(). Please provide values below: