Export (0) Print
Expand All


Inserts an element constructed in place (no copy or move operations are performed).

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




The arguments forwarded to construct an element to be inserted into the multimap.

An iterator to the newly inserted element.

No references to container elements are invalidated by this function, but it may invalidate all iterators to the container.

If an exception is thrown during the insertion, the container is left unaltered and the exception is rethrown.

The value_type of an element is a pair, so that the value of an element will be an ordered pair with the first component equal to the key value and the second component equal to the data value of the element.

// multimap_emplace.cpp
// compile with: /EHsc
#include <map>
#include <string>
#include <iostream>

using namespace std;

template <typename M> void print(const M& m) {
    cout << m.size() << " elements: " << endl;

    for (const auto& p : m) {
        cout << "(" << p.first <<  "," << p.second << ") ";

    cout << endl;

int main()
    multimap<string, string> m1;

    m1.emplace("Anna", "Accounting");
    m1.emplace("Bob", "Accounting");
    m1.emplace("Carmine", "Engineering");

    cout << "multimap modified, now contains ";
    cout << endl;

    m1.emplace("Bob", "Engineering");

    cout << "multimap modified, now contains ";
    cout << endl;

multimap modified, now contains 3 elements:
(Anna,Accounting) (Bob,Accounting) (Carmine,Engineering)

multimap modified, now contains 4 elements:
(Anna,Accounting) (Bob,Accounting) (Bob,Engineering) (Carmine,Engineering)

Header: <map>

Namespace: std

© 2014 Microsoft