C++ Unordered_multimap Library - emplace() Function


Advertisements

Description

The C++ function std::unordered_multimap::emplace() extends container by inserting new element.

This member function increases the container size by one.

Declaration

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

C++11

template <class... Args>
iterator emplace(Args&&... args );

Parameters

args − arguments to forward to the constructor of the element.

Return value

Returns an iterator to newly inserted element.

Example

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

#include <iostream>
#include <unordered_map>

using namespace std;

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

   umm.emplace('b', 2);
   umm.emplace('c', 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 = 5
a = 1
b = 2
b = 2
c = 3
c = 3
d = 4
unordered_map.htm
Advertisements