C++ Unordered_multimap Library - emplace_hint() Function


The C++ function std::unordered_multimap::emplace_hint() inserts a new element in a unordered_multimap using hint as a position for element.


Following is the declaration for std::unordered_multimap::emplace_hint() function form std::unordered_map() header.


template <class... Args>
iterator emplace_hint(const_iterator position, Args&&... args );


  • position − Hint for the position to insert element.

  • args − Arguments forwarded to construct the new element.

Return value

Returns an iterator to the newly inserted element.

Time complexity

Constant i.e. O(1) in average case.

Linear i.e. O(n) in container size in worst case.


The following example shows the usage of std::unordered_multimap::emplace_hint() function.

#include <iostream>
#include <unordered_map>

using namespace std;

int main(void) {
   unordered_multimap<char, int> umm = {
            {'b', 2},
            {'c', 3},
            {'d', 4},

   umm.emplace_hint(umm.begin(), 'b', 2);
   umm.emplace_hint(umm.end(), 'e', 3);

   cout << "Unordered multimap contains following elements" << endl;

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

   return 0;

Let us compile and run the above program, this will produce the following result −

Unordered multimap contains following elements
e = 3
d = 4
b = 2
b = 2
c = 3