std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: try_emplace
|
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
로 생성된 값을 가진 새 요소를 삽입합니다.
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)...);
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)...);
key_type
으로의 변환은 객체
u
를 생성해야 하며, 이에 대해
find
(
k
)
==
find
(
u
)
가
true
여야 합니다. 그렇지 않으면 동작은 정의되지 않습니다.
-
한정자 ID
Compare::is_transparent가 유효하고 타입을 나타내는 경우. - std:: is_constructible_v < key_type, K > 가 true 인 경우.
- std:: is_assignable_v < mapped_type & , Args... > 가 true 인 경우.
- 오버로드 (3) 에 대해서만, std:: is_convertible_v < K && , const_iterator > 와 std:: is_convertible_v < K && , iterator > 가 모두 false 인 경우.
| 반복자 무효화에 대한 정보는 여기 에서 복사되었습니다 |
목차 |
매개변수
| k | - | 검색에 사용되며, 찾지 못한 경우 삽입에도 사용되는 키 |
| hint | - | 새 요소가 삽입될 위치 바로 앞을 가리키는 반복자 |
| args | - | 요소의 생성자로 전달할 인자들 |
반환값
emplace
와 동일합니다.
emplace_hint
와 동일합니다.
복잡도
emplace
와 동일합니다.
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) |