std::unordered_set<Key,Hash,KeyEqual,Allocator>:: emplace_hint
|
template
<
class
...
Args
>
iterator emplace_hint ( const_iterator hint, Args && ... args ) ; |
(C++11부터)
(C++26부터 constexpr) |
|
컨테이너에 새 요소를 삽입하며, hint 를 요소가 위치해야 할 곳에 대한 제안으로 사용합니다.
키와 매핑된 값의 생성자는 함수에 제공된 인수와 정확히 동일한 인수로 호출되며, std:: forward < Args > ( args ) ... 와 함께 전달됩니다.
만약 연산 후 새로운 원소의 개수가 기존
max_load_factor()
*
bucket_count()
보다 크면 재해싱이 발생합니다.
재해싱이 발생하는 경우(삽입으로 인해), 모든 반복자는 무효화됩니다. 그렇지 않은 경우(재해싱 없음), 반복자는 무효화되지 않습니다.
목차 |
매개변수
| hint | - | 반복자, 새로운 요소를 삽입할 위치에 대한 제안으로 사용됨 |
| args | - | 요소의 생성자에게 전달할 인자들 |
반환값
삽입된 요소 또는 삽입을 방해한 요소에 대한 반복자.
예외
어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
복잡도
평균적으로 분할 상환된 상수 시간, 최악의 경우 컨테이너 크기에 선형적으로 증가합니다.
예제
#include <chrono> #include <cstddef> #include <functional> #include <iomanip> #include <iostream> #include <unordered_set> const int n_operations = 1005000; std::size_t set_emplace() { std::unordered_set<int> set; for (int i = 0; i < n_operations; ++i) set.emplace(i); return set.size(); } std::size_t set_emplace_hint() { std::unordered_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::unordered_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::unordered_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::unordered_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(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); }
가능한 출력:
146.88ms for plain emplace (ratio: 1.00) 168.20ms for emplace with correct hint (ratio: 0.87) 168.78ms for emplace with wrong hint (ratio: 0.87) 166.58ms for corrected emplace (ratio: 0.88) 168.27ms for emplace using returned iterator (ratio: 0.87)
참고 항목
|
제자리에서 요소 생성
(public member function) |
|
|
요소 삽입
또는 노드
(since C++17)
(public member function) |