std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: try_emplace
|
template
<
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( const Key & k, Args && ... args ) ; |
(1) | (C++17부터) |
|
template
<
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( Key && k, Args && ... args ) ; |
(2) | (C++17부터) |
|
template
<
class
K,
class
...
Args
>
std:: pair < iterator, bool > try_emplace ( K && k, Args && ... args ) ; |
(3) | (C++26부터) |
|
template
<
class
...
Args
>
iterator try_emplace ( const_iterator hint, const Key & k, Args && ... args ) ; |
(4) | (C++17부터) |
|
template
<
class
...
Args
>
iterator try_emplace ( const_iterator hint, Key && k, Args && ... args ) ; |
(5) | (C++17부터) |
|
template
<
class
K,
class
...
Args
>
iterator try_emplace ( const_iterator hint, K && k, Args && ... args ) ; |
(6) | (C++26부터) |
만약 컨테이너에 k 와 동등한 키가 이미 존재하면, 아무 작업도 수행하지 않습니다. 그렇지 않으면, k 를 키로 하고 args 로 생성된 값을 가진 새 요소를 컨테이너에 삽입합니다. 이 경우:
emplace
와 유사하게 동작하지만, 요소가 다음과 같이 생성됩니다:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace
와 유사하게 동작하지만, 요소가 다음과 같이 생성됩니다:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace
와 유사하게 동작하지만, 요소가 다음과 같이 생성됩니다:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
emplace_hint
와 동일하게 동작하지만, 요소가 다음과 같이 구성됩니다
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
k
)
,
emplace_hint
와 유사하게 동작하지만, 요소가 다음과 같이 생성됩니다:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std
::
move
(
k
)
)
,
emplace_hint
와 동일하게 동작하지만, 요소가 다음과 같이 생성됩니다:
value_type ( std:: piecewise_construct ,
std::
forward_as_tuple
(
std::
forward
<
K
>
(
k
)
)
,
- std:: is_convertible_v < K && , const_iterator > 와 std:: is_convertible_v < K && , iterator > 가 모두 false 인 경우.
- Hash :: is_transparent 와 KeyEqual :: is_transparent 가 유효하고 각각 타입을 나타내는 경우.
만약 연산 후 새로운 원소의 개수가 기존
max_load_factor()
*
bucket_count()
보다 크면 재해싱이 발생합니다.
재해싱이 발생하는 경우(삽입으로 인해), 모든 반복자는 무효화됩니다. 그렇지 않은 경우(재해싱 없음), 반복자는 무효화되지 않습니다.
목차 |
매개변수
| k | - | 검색에 사용되며, 찾지 못한 경우 삽입에 사용되는 키 |
| hint | - | 새 요소가 삽입될 위치 바로 앞을 가리키는 반복자 |
| args | - | 요소의 생성자로 전달할 인자들 |
반환값
복잡도
참고 사항
insert
나
emplace
와 달리, 이러한 함수들은 삽입이 발생하지 않을 경우 rvalue 인수로부터 이동을 수행하지 않습니다. 이로 인해 값이 이동 전용 타입인 맵(예:
std::
unordered_map
<
std::
string
,
std::
unique_ptr
<
foo
>>
)을 조작하기 쉽습니다. 또한
try_emplace
는 키와
mapped_type
에 대한 인수를 별도로 처리하는 반면,
emplace
는
value_type
(즉,
std::pair
)을 구성하기 위한 인수를 요구합니다.
오버로드
(
3
)
와
(
6
)
는
Key
타입의 객체를 생성하지 않고 호출할 수 있습니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_unordered_map_try_emplace
|
201411L
|
(C++17) |
std::unordered_map::try_emplace
,
std::unordered_map::insert_or_assign |
__cpp_lib_associative_heterogeneous_insertion
|
202311L
|
(C++26) | 연관 컨테이너에 남아있는 멤버 함수들에 대한 이종 오버로드 정렬된 및 비정렬 연관 컨테이너 . 오버로드 ( 3 ) 및 ( 6 ) . |
예제
#include <iostream> #include <string> #include <unordered_map> #include <utility> void print_node(const auto& node) { std::cout << '[' << node.first << "] = " << node.second << '\n'; } void print_result(auto const& pair) { std::cout << (pair.second ? "inserted: " : "ignored: "); print_node(*pair.first); } int main() { using namespace std::literals; std::unordered_map<std::string, std::string> m; print_result(m.try_emplace("a", "a"s)); print_result(m.try_emplace("b", "abcd")); print_result(m.try_emplace("c", 10, 'c')); print_result(m.try_emplace("c", "Won't be inserted")); for (const auto& p : m) print_node(p); }
가능한 출력:
inserted: [a] = a inserted: [b] = abcd inserted: [c] = cccccccccc ignored: [c] = cccccccccc [a] = a [b] = abcd [c] = cccccccccc
참고 항목
|
제자리에서 요소를 생성합니다
(public member function) |
|
|
힌트를 사용하여 제자리에서 요소를 생성합니다
(public member function) |
|
|
요소를 삽입합니다
또는 노드
(C++17부터)
(public member function) |