Partager via


multiset::emplace

Inserts an element constructed in place (no copy or move operations are performed), with a placement hint.

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

Parameters

Parameter

Description

args

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

Return Value

An iterator to the newly inserted element.

Remarks

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

During emplacement, if an exception is thrown, the container's state is not modified.

Example

// multiset_emplace.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    multiset<string> s1;

    s1.emplace("Anna");
    s1.emplace("Bob");
    s1.emplace("Carmine");

    cout << "multiset modified, now contains ";
    print(s1);
    cout << endl;

    s1.emplace("Bob");

    cout << "multiset modified, now contains ";
    print(s1);
    cout << endl;
}

Output

multiset modified, now contains 3 elements: (Anna) (Bob) (Carmine)

multiset modified, now contains 4 elements: (Anna) (Bob) (Bob) (Carmine)

Requirements

Header: <set>

Namespace: std

See Also

Reference

<set>

set Class

Standard Template Library