Namespaces
Variants

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: try_emplace

From cppreference.net
template < class ... Args >
std:: pair < iterator, bool > try_emplace ( const key_type & k, Args && ... args ) ;
(1) (C++23부터)
template < class ... Args >
std:: pair < iterator, bool > try_emplace ( key_type && k, Args && ... args ) ;
(2) (C++23부터)
template < class K, class ... Args >
std:: pair < iterator, bool > try_emplace ( K && k, Args && ... args ) ;
(3) (C++23부터)
template < class ... Args >
iterator try_emplace ( const_iterator hint, const key_type & k, Args && ... args ) ;
(4) (C++23부터)
template < class ... Args >
iterator try_emplace ( const_iterator hint, key_type && k, Args && ... args ) ;
(5) (C++23부터)
template < class K, class ... Args >
iterator try_emplace ( const_iterator hint, K && k, Args && ... args ) ;
(6) (C++23부터)

만약 컨테이너에 k 와 동등한 키가 이미 존재하면, 아무 작업도 수행하지 않습니다. 그렇지 않으면, 기본 컨테이너 c k 키와 args 로 생성된 값을 가진 새 요소를 삽입합니다.

1,2,4,5) 다음과 동등함:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it);
c.keys.insert(key_it, std::forward<decltype(k)>(k));
c.values.emplace(value_it, std::forward<Args>(args)...);
3,6) 다음과 동일함:
auto key_it = ranges::upper_bound(c.keys, k, compare);
auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it);
c.keys.emplace(key_it, std::forward<K>(k));
c.values.emplace(value_it, std::forward<Args>(args)...);
k 에서 key_type 으로의 변환은 객체 u 를 생성해야 하며, 이에 대해 find ( k ) == find ( u ) true 여야 합니다. 그렇지 않으면 동작은 정의되지 않습니다.
다음 오버로드들은 다음 조건에서만 오버로드 해결에 참여합니다:

목차

매개변수

k - 검색에 사용되며, 찾지 못한 경우 삽입에도 사용되는 키
hint - 새 요소가 삽입될 위치 바로 앞을 가리키는 반복자
args - 요소의 생성자로 전달할 인자들

반환값

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

복잡도

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

참고 사항

insert emplace 와 달리, 이러한 함수들은 삽입이 발생하지 않을 경우 rvalue 인수로부터 이동을 수행하지 않습니다. 이로 인해 값이 이동 전용 타입인 맵(예: std:: flat_map < std:: string , std:: unique_ptr < foo >> )을 쉽게 조작할 수 있습니다. 또한 try_emplace emplace 와 달리 키와 mapped_type 에 대한 인수를 별도로 처리합니다. emplace value_type (즉, std::pair )을 생성하기 위한 인수가 필요합니다.

오버로드 ( 3,6 ) key_type 타입의 객체를 생성하지 않고 호출할 수 있습니다.

예제

#include <flat_map>
#include <iostream>
#include <string>
#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::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)
요소 삽입
(public member function)
요소를 삽입하거나 키가 이미 존재하는 경우 현재 요소에 할당
(public member function)