Namespaces
Variants

std::multiset<Key,Compare,Allocator>:: emplace

From cppreference.net

template < class ... Args >
iterator emplace ( Args && ... args ) ;
(C++11부터)
(C++26부터 constexpr)

주어진 args 로 컨테이너 내부에서 제자리 생성되는 새로운 요소를 삽입합니다.

새로운 요소의 생성자는 emplace 에 제공된 인수와 정확히 동일한 인수로 호출되며, std:: forward < Args > ( args ) ... 를 통해 전달됩니다.

만약 value_type EmplaceConstructible 가 아니어서 multiset args 로부터 생성될 수 없는 경우, 동작은 정의되지 않습니다.

반복자나 참조가 무효화되지 않습니다.

목차

매개변수

args - 요소의 생성자에 전달할 인수들

반환값

삽입된 원소에 대한 반복자.

예외

어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

복잡도

컨테이너 크기에 대해 로그 시간 복잡도를 가집니다.

참고 사항

emplace 를 신중하게 사용하면 새로운 요소가 불필요한 복사 또는 이동 연산을 피하면서 생성될 수 있습니다.

예제

#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <set>
class Dew
{
private:
    int a, b, c;
public:
    Dew(int _a, int _b, int _c)
        : a(_a), b(_b), c(_c)
    {}
    bool operator<(const Dew& other) const
    {
        return (a < other.a) ||
               (a == other.a && b < other.b) ||
               (a == other.a && b == other.b && c < other.c);
    }
};
constexpr int nof_operations{101};
std::size_t set_emplace()
{
    std::multiset<Dew> set;
    for (int i = 0; i < nof_operations; ++i)
        for (int j = 0; j < nof_operations; ++j)
            for (int k = 0; k < nof_operations; ++k)
                set.emplace(i, j, k);
    return set.size();
}
std::size_t set_insert()
{
    std::multiset<Dew> set;
    for (int i = 0; i < nof_operations; ++i)
        for (int j = 0; j < nof_operations; ++j)
            for (int k = 0; k < nof_operations; ++k)
                set.insert(Dew(i, j, k));
    return set.size();
}
void time_it(std::function<int()> set_test, std::string what = "")
{
    const auto start = std::chrono::system_clock::now();
    const auto the_size = set_test();
    const auto stop = std::chrono::system_clock::now();
    const std::chrono::duration<double, std::milli> time = stop - start;
    if (!what.empty() && the_size)
        std::cout << std::fixed << std::setprecision(2)
                  << time << " for " << what << '\n';
}
int main()
{
    time_it(set_insert, "cache warming...");
    time_it(set_insert, "insert");
    time_it(set_insert, "insert");
    time_it(set_emplace, "emplace");
    time_it(set_emplace, "emplace");
}

가능한 출력:

499.61ms for cache warming...
447.89ms for insert
436.77ms for insert
430.62ms for emplace
428.61ms for emplace

참고 항목

힌트를 사용하여 제자리에서 요소를 생성합니다
(public member function)
요소를 삽입합니다 또는 노드 (since C++17)
(public member function)