Partager via


unordered_map::emplace

Adds an element constructed in place.

template<class ValTy>
    pair<iterator, bool> emplace(ValTy&& val);

Parameters

Parameter

Description

ValTy

The in-place constructor argument type.

val

Value to insert.

Remarks

The member function determines whether an element X exists in the sequence whose key has equivalent ordering to that of val. If not, it constructs such an element X with val. The function then determines the iterator where that designates X. If an insertion occurred, the function returns std::pair(where, true). Otherwise, it returns std::pair(where, false).

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

Example

// std_tr1__unordered_map__unordered_map_emplace.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
#include <string>
 
int main() 
    { 
    using namespace std;
    unordered_map<int, string> c1;
    pair<int, string> is1(1, "a");

    c1.emplace(move(is1));
    cout << "After the emplace insertion, c1 contains:" << endl
      << " " << c1.begin()->first
      << " => " << c1.begin()->second
      << endl;

    return (0); 
    } 
 
After the emplace insertion, c1 contains:
 1 => a

Requirements

Header: <unordered_map>

Namespace: std

See Also

Reference

<unordered_map>

unordered_map Class

Other Resources

<unordered_map> Members