Namespaces
Variants

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: emplace_hint

From cppreference.net

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

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

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

목차

매개변수

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

반환값

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

예외

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

복잡도

예제

#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <flat_map>
const int n_operations = 100'500'0;
std::size_t map_emplace()
{
    std::flat_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::flat_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::flat_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::flat_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::flat_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); }

가능한 출력:

...할 일...

참고 항목

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