std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: emplace_hint
|
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
)
...
를 통해 전달됩니다.
만약 연산 후 새로운 원소의 개수가 기존
max_load_factor()
*
bucket_count()
보다 크면 재해싱이 발생합니다.
재해싱이 발생하는 경우(삽입으로 인해), 모든 반복자는 무효화됩니다. 그렇지 않은 경우(재해싱 없음), 반복자는 무효화되지 않습니다.
목차 |
매개변수
| hint | - | iterator, 새 요소를 삽입할 위치에 대한 제안으로 사용됨 |
| args | - | 요소의 생성자에게 전달할 인자들 |
반환값
삽입된 요소 또는 삽입을 방해한 요소에 대한 반복자.
예외
어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
복잡도
평균적으로 분할 상환된 상수 시간, 최악의 경우 컨테이너 크기에 선형적으로 증가합니다.
예제
#include <chrono>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <unordered_map>
const int n_operations = 100'500'0;
std::size_t map_emplace()
{
std::unordered_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::unordered_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::unordered_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::unordered_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::unordered_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 태그와 속성은 그대로 유지
- ``, ``, `
<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);
}
가능한 출력:
143.48ms for plain emplace (ratio: 1.00) 164.78ms for emplace with correct hint (ratio: 0.87) 171.22ms for emplace with wrong hint (ratio: 0.84) 166.55ms for corrected emplace (ratio: 0.86) 167.41ms for emplace using returned iterator (ratio: 0.86)
참고 항목
|
제자리에서 요소 생성
(public member function) |
|
|
요소 삽입
또는 노드
(C++17부터)
(public member function) |