Namespaces
Variants

std::flat_set<Key,Compare,KeyContainer>:: emplace_hint

From cppreference.net

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

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

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

목차

매개변수

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

반환값

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

예외

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

복잡도

예제

#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <flat_set>
const int n_operations = 1005000;
std::size_t set_emplace()
{
    std::flat_set<int> set;
    for (int i = 0; i < n_operations; ++i)
        set.emplace(i);
    return set.size();
}
std::size_t set_emplace_hint()
{
    std::flat_set<int> set;
    auto it = set.begin();
    for (int i = 0; i < n_operations; ++i)
    {
        set.emplace_hint(it, i);
        it = set.end();
    }
    return set.size();
}
std::size_t set_emplace_hint_wrong()
{
    std::flat_set<int> set;
    auto it = set.begin();
    for (int i = n_operations; i > 0; --i)
    {
        set.emplace_hint(it, i);
        it = set.end();
    }
    return set.size();
}
std::size_t set_emplace_hint_corrected()
{
    std::flat_set<int> set;
    auto it = set.begin();
    for (int i = n_operations; i > 0; --i)
    {
        set.emplace_hint(it, i);
        it = set.begin();
    }
    return set.size();
}
std::size_t set_emplace_hint_closest()
{
    std::flat_set<int> set;
    auto it = set.begin();
    for (int i = 0; i < n_operations; ++i)
        it = set.emplace_hint(it, i);
    return set.size();
}
double time_it(std::function<std::size_t()> set_test,
               const char* what = nullptr,
               double ratio = 0.0)
{
    const auto start = std::chrono::system_clock::now();
    const std::size_t setsize = set_test();
    const auto stop = std::chrono::system_clock::now();
    const std::chrono::duration
(설명: HTML 태그와 속성은 번역하지 않고 그대로 유지되었으며, C++ 관련 용어(std::chrono::duration)는 번역되지 않았습니다. 링크 구조와 클래스명이 원본 형식을 그대로 보존되었습니다.)<double, std::milli> time = stop - start;
    if (what != nullptr && setsize > 0)
        std::cout << std::setw
**변역 설명:**
- HTML 태그와 속성은 그대로 유지
- `` 태그 내의 텍스트 중 C++ 관련 용어(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(set_emplace); // 캐시 워밍업
    const auto x = time_it(set_emplace, "plain emplace");
    time_it(set_emplace_hint, "올바른 힌트와 함께 배치", x);
    time_it(set_emplace_hint_wrong, "잘못된 힌트와 함께 배치", x);
    time_it(set_emplace_hint_corrected, "수정된 emplace", x);
    time_it(set_emplace_hint_closest, "반환된 반복자를 사용하여 배치", x);
}

가능한 출력:

...할 일...

참고 항목

제자리에서 요소 생성
(public member function)
요소 삽입
(public member function)