multimap::emplace_hint() in C++ STL

C++Server Side ProgrammingProgramming

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

What is Multimap in C++ STL?

Multimaps are the associative containers, which are similar to map containers. It also facilitates to 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::emplace_hint()?

emplace_hint() function is an inbuilt function in C++ STL, which is defined in <map> header file. This function inserts a new element in the multimap container with a position. In emplace_hint() we pass the element with a position, the position acts as a hint. This function is similar to emplace() the difference is we give a position hint to insert the value. This function also increases the size of the multiset container by 1.


multimap_name.emplace_hint(iterator pos, Args& val);


The function accepts following parameter(s)−

  • pos − This is the iterator type of argument which is used to give the position hint.

  • val− This is the element which we want to insert.

Return value

This function returns an iterator to the position of where the element is emplaced/inserted.


std::multimap<char, int> odd, eve;
odd.insert({‘a’, 1});
odd.insert({‘b’, 3});
odd.insert({‘c’, 5});
odd.emplace_hint(odd.end(), {‘d’, 7});


Odd: a:1 b:3 c:5 d:7


 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //create the container
   multimap<int, int> mul;
   //insert using emplace
   mul.emplace_hint(mul.begin(), 1, 10);
   mul.emplace_hint(mul.begin(), 2, 20);
   mul.emplace_hint(mul.begin(), 3, 30);
   mul.emplace_hint(mul.begin(), 1, 40);
   mul.emplace_hint(mul.begin(), 4, 50);
   mul.emplace_hint(mul.begin(), 5, 60);
   cout << "\nElements in multimap is : \n";
   cout << "KEY\tELEMENT\n";
   for (auto i = mul.begin(); i!= mul.end(); i++){
      cout << i->first << "\t" << i->second << endl;
   return 0;


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

Elements in multimap is :
1 40
1 10
2 20
3 30
4 50
5 60
Published on 22-Apr-2020 11:46:06