std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: insert_or_assign
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( const Key & k, M && obj ) ; |
(1) | (C++17부터) |
|
template
<
class
M
>
std:: pair < iterator, bool > insert_or_assign ( Key && k, M && obj ) ; |
(2) | (C++17부터) |
|
template
<
class
K,
class
M
>
std:: pair < iterator, bool > insert_or_assign ( K && k, M && obj ) ; |
(3) | (C++26부터) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, const Key & k, M && obj ) ; |
(4) | (C++17부터) |
|
template
<
class
M
>
iterator insert_or_assign ( const_iterator hint, Key && k, M && obj ) ; |
(5) | (C++17부터) |
|
template
<
class
K,
class
M
>
iterator insert_or_assign ( const_iterator hint, K && k, M && obj ) ; |
(6) | (C++26부터) |
mapped_type
에 할당합니다. 키가 존재하지 않는 경우,
value_type
(
k,
std::
forward
<
M
>
(
obj
)
)
에서 생성하여
insert
를 수행하는 것처럼 새로운 값을 삽입합니다.
mapped_type
에 할당합니다. 키가 존재하지 않는 경우,
value_type
의 객체
u
를
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
)
로 생성한 후
u
를
*
this
에 삽입합니다.
hash_function
(
)
(
u.
first
)
!
=
hash_function
(
)
(
k
)
||
contains
(
u.
first
)
가
true
인 경우 동작은 정의되지 않습니다.
value_type
은
std::
forward
<
K
>
(
k
)
,
std::
forward
<
M
>
(
obj
)
로부터
unordered_map
에 대해
EmplaceConstructible
이어야 합니다. 이 오버로드는
Hash
와
KeyEqual
이 모두
transparent
인 경우에만 오버로드 해결에 참여합니다. 이는
Hash
가
K
와
Key
타입 모두에 대해 호출 가능하고,
KeyEqual
이 투명하다는 것을 가정하며, 이를 통해
Key
의 인스턴스를 생성하지 않고도 이 함수를 호출할 수 있습니다.
동작은 정의되지 않음 (C++20 이전) 프로그램의 형식이 올바르지 않음 (C++20 이후) 만약 std:: is_assignable_v < mapped_type & , M && > 가 false 인 경우.
만약 연산 후 새로운 원소의 개수가 기존
max_load_factor()
*
bucket_count()
보다 크면 재해싱이 발생합니다.
재해싱이 발생하는 경우(삽입으로 인해), 모든 반복자는 무효화됩니다. 그렇지 않은 경우(재해싱 없음), 반복자는 무효화되지 않습니다.
목차 |
매개변수
| k | - | 검색에 사용되며, 찾지 못한 경우 삽입에 사용되는 키 |
| hint | - | 새 요소가 삽입될 위치 바로 앞을 가리키는 반복자 |
| obj | - | 삽입 또는 할당할 값 |
반환값
복잡도
emplace
와 동일합니다.
emplace_hint
와 동일합니다.
참고 사항
insert_or_assign
는
operator
[
]
보다 더 많은 정보를 반환하며, 매핑된 타입의 기본 생성 가능성을 요구하지 않습니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__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> void print_node(const auto& node) { std::cout << '[' << node.first << "] = " << node.second << '\n'; } void print_result(auto const& pair) { std::cout << (pair.second ? "inserted: " : "assigned: "); print_node(*pair.first); } int main() { std::unordered_map<std::string, std::string> myMap; print_result(myMap.insert_or_assign("a", "apple")); print_result(myMap.insert_or_assign("b", "banana")); print_result(myMap.insert_or_assign("c", "cherry")); print_result(myMap.insert_or_assign("c", "clementine")); for (const auto& node : myMap) print_node(node); }
가능한 출력:
inserted: [a] = apple inserted: [b] = banana inserted: [c] = cherry assigned: [c] = clementine [c] = clementine [a] = apple [b] = banana
참고 항목
|
지정된 요소에 접근하거나 삽입
(public member function) |
|
|
범위 검사와 함께 지정된 요소에 접근
(public member function) |
|
|
요소 삽입
또는 노드
(since C++17)
(public member function) |
|
|
요소를 제자리에 생성
(public member function) |