Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: emplace_hint

From cppreference.net

template < class ... Args >
iterator emplace_hint ( const_iterator hint, Args && ... args ) ;
(C++11부터)
(C++26부터 constexpr)

컨테이너에 새로운 요소를 가능한 한 hint 바로 앞 위치에 가깝게 삽입합니다.

value_type 의 생성자(즉, std:: pair < const Key, T > )는 함수에 제공된 인자와 정확히 동일한 인자로 호출되며, std:: forward < Args > ( args ) ... 를 통해 전달됩니다.

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

목차

매개변수

hint - 새로운 요소가 삽입될 위치 바로 앞을 가리키는 반복자
args - 요소의 생성자에게 전달할 인자들

반환값

삽입된 요소 또는 삽입을 방해한 요소에 대한 반복자.

예외

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

복잡도

컨테이너 크기에 대해 일반적으로 로그 시간이지만, 새로운 요소가 hint 바로 앞에 삽입되는 경우 분할 상환 상수 시간입니다.

예제

#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
const int n_operations = 100'500'0;
std::size_t map_emplace()
{
    std::map<int, char> map;
    for (int i = 0; i < n_operations; ++i)
        map.emplace(i, 'a');
    return map.size();
}
std::size_t map_emplace_hint()
{
    std::map<int, char> map;
    auto it = map.begin();
    for (int i = 0; i < n_operations; ++i)
    {
        map.emplace_hint(it, i, 'b');
        it = map.end();
    }
    return map.size();
}
std::size_t map_emplace_hint_wrong()
{
    std::map<int, char> map;
    auto it = map.begin();
    for (int i = n_operations; i > 0; --i)
    {
        map.emplace_hint(it, i, 'c');
        it = map.end();
    }
    return map.size();
}
std::size_t map_emplace_hint_corrected()
{
    std::map<int, char> map;
    auto it = map.begin();
    for (int i = n_operations; i > 0; --i)
    {
        map.emplace_hint(it, i, 'd');
        it = map.begin();
    }
    return map.size();
}
std::size_t map_emplace_hint_closest()
{
    std::map<int, char> map;
    auto it = map.begin();
    for (int i = 0; i < n_operations; ++i)
        it = map.emplace_hint(it, i, 'e');
    return map.size();
}
double time_it(std::function<std::size_t()> map_test,
               std::string what = "", double ratio = 0.0)
{
    const auto start = std::chrono::system_clock::now();
    const std::size_t map_size = map_test();
    const auto stop = std::chrono::system_clock::now();
    std::chrono::duration
(설명: HTML 태그와 속성은 그대로 유지되었으며, C++ 관련 용어(std::chrono::duration)는 번역되지 않았습니다. 링크 구조와 클래스 속성도 원본 형식을 그대로 보존하였습니다.)<double, std::milli> time = stop - start;
    if (what.size() && map_size)
        std::cout << std::setw(8) << time << " for " << what << " (비율: "
                  << (ratio == 0.0 ? 1.0 : ratio / time.count()) << ")\n";
    return time.count();
}
int main()
{
    std::cout << std::fixed << std::setprecision(2);
    time_it(map_emplace); // 캐시 워밍업
    const auto x = time_it(map_emplace, "plain emplace");
    time_it(map_emplace_hint, "올바른 힌트와 함께 배치", x);
    time_it(map_emplace_hint_wrong, "잘못된 힌트와 함께 배치", x);
    time_it(map_emplace_hint_corrected, "수정된 emplace", x);
    time_it(map_emplace_hint_closest, "반환된 반복자를 사용하여 배치", x);
}

가능한 출력:

365.08ms for plain emplace (ratio: 1.00)
 96.54ms for emplace with correct hint (ratio: 3.78)
409.40ms for emplace with wrong hint (ratio: 0.89)
 85.57ms for corrected emplace (ratio: 4.27)
 84.31ms for emplace using returned iterator (ratio: 4.33)

참고 항목

(C++11)
제자리에서 요소 생성
(public member function)
요소 삽입 또는 노드 (C++17부터)
(public member function)