Namespaces
Variants

std::map<Key,T,Compare,Allocator>:: insert_or_assign

From cppreference.net

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부터)
1,4) 컨테이너에 k 와 동등한 키가 이미 존재하는 경우, std:: forward < M > ( obj ) 를 키 k 에 해당하는 mapped_type 에 할당합니다. 키가 존재하지 않는 경우, value_type ( k, std:: forward < M > ( obj ) ) 로부터 생성하여 insert 를 수행하는 것처럼 새로운 값을 삽입합니다.
2,5) (1,4) 와 동일하지만, 매핑된 값은 value_type ( std :: move ( k ) , std:: forward < M > ( obj ) ) 로부터 생성됩니다.
3,6) 컨테이너에 이미 k 와 동등한 키가 존재하는 경우, std:: forward < M > ( obj ) 를 키 k 에 대응하는 mapped_type 에 할당합니다. 키가 존재하지 않는 경우, value_type 의 객체 u std:: forward < K > ( k ) , std:: forward < M > ( obj ) ) 로 생성한 후 u * this 에 삽입합니다. equal_range ( u. first ) == equal_range ( k ) false 인 경우 동작은 정의되지 않습니다. value_type std:: forward < K > ( k ) , std:: forward < M > ( obj ) 로부터 map 안으로 EmplaceConstructible 이어야 합니다. 이 오버로드는 Compare transparent 일 때만 오버로드 해결에 참여합니다. 이를 통해 Key 의 인스턴스를 생성하지 않고 이 함수를 호출할 수 있습니다.

동작은 정의되지 않음 (C++20 이전) 프로그램의 형식이 올바르지 않음 (C++20 이후) 만약 std:: is_assignable_v < mapped_type & , M && > false 인 경우.

반복자나 참조가 무효화되지 않습니다.

목차

매개변수

k - 검색에 사용되며, 찾지 못한 경우 삽입에 사용되는 키
hint - 새 요소가 삽입될 위치 바로 앞을 가리키는 반복자
obj - 삽입 또는 할당할 값

반환값

1-3) bool 구성 요소는 삽입이 발생한 경우 true 이고 할당이 발생한 경우 false 입니다. 반복자 구성 요소는 삽입되거나 업데이트된 요소를 가리킵니다.
4-6) 삽입되거나 업데이트된 요소를 가리키는 반복자.

복잡도

1-3) emplace 와 동일합니다.
4-6) emplace_hint 와 동일합니다.

참고 사항

insert_or_assign operator [ ] 보다 더 많은 정보를 반환하며, 매핑된 타입의 기본 생성 가능성을 요구하지 않습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_map_try_emplace 201411L (C++17) std::map::try_emplace , std::map::insert_or_assign
__cpp_lib_associative_heterogeneous_insertion 202311L (C++26) 정렬된 비정렬 연관 컨테이너 에서 남아있는 멤버 함수들에 대한 이종 오버로드. ( 3 ) ( 6 ) 오버로드.

예제

#include <iostream>
#include <string>
#include <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::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
[a] = apple
[b] = banana
[c] = clementine

참고 항목

지정된 요소에 접근하거나 삽입
(public member function)
범위 검사와 함께 지정된 요소에 접근
(public member function)
요소 삽입 또는 노드 (C++17부터)
(public member function)
(C++11)
제자리에서 요소 구성
(public member function)