Share via


unordered_set::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 of a single element, the container is left unaltered and the exception is rethrown. If an exception is thrown during the insertion of multiple elements, the container is left in a stable but unspecified state and the exception is rethrown.

Example

 

// std_tr1__unordered_set__unordered_set_emplace.cpp 
// compile with: /EHsc 
#include <unordered_set> 
#include <iostream>
#include <string> 
 
int main() 
    { 
    unordered_set< string> c1;
    string str1("a");

    c1.emplace(move(str1));
    cout << "After the emplace insertion, c1 contains: "
        << *c1.begin() << endl;

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

Requirements

Header: <unordered_set>

Namespace: std

See Also

Reference

<unordered_set>

unordered_set Class

Other Resources

<unordered_set> Members