std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>:: emplace
|
template
<
class
...
Args
>
iterator emplace ( Args && ... args ) ; |
(C++23부터)
(C++26부터 constexpr) |
|
주어진 args 로 컨테이너 내부에서 제자리 생성되는 새로운 요소를 삽입합니다.
객체 t 를 std:: pair < key_type, mapped_type > 타입으로 초기화하며, std:: forward < Args > ( args ) ... 를 사용합니다. 만약 맵이 이미 t. first 와 동등한 키를 가진 요소를 포함하고 있다면, * this 는 변경되지 않습니다. 그렇지 않으면, 다음 코드와 동일합니다:
auto key_it = ranges::upper_bound(c.keys, t.first, compare); auto value_it = c.values.begin() + std::distance(c.keys.begin(), key_it); c.keys.insert(key_it, std::move(t.first)); c.values.insert(value_it, std::move(t.second));
이 오버로드는 다음 조건이 std:: is_constructible_v < std:: pair < key_type, mapped_type > , Args... > 일 때만 오버로드 해결에 참여합니다.
만약
value_type
이
EmplaceConstructible
가 아니어서
flat_multimap
에
args
로부터 생성할 수 없는 경우, 동작은 정의되지 않습니다.
| 반복자 무효화에 대한 정보는 여기 에서 복사되었습니다 |
목차 |
매개변수
| args | - | 요소의 생성자에 전달할 인수들 |
반환값
삽입된 요소에 대한 반복자.
예외
어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
복잡도
컨테이너 크기에 선형적으로 비례
참고 사항
emplace
를 신중하게 사용하면 새로운 요소가 불필요한 복사 또는 이동 연산을 피하면서 생성될 수 있습니다.
예제
#include <iostream> #include <string> #include <utility> #include <flat_map> int main() { std::flat_multimap<std::string, std::string> m; // pair의 이동 생성자 사용 m.emplace(std::make_pair(std::string("a"), std::string("a"))); // pair의 변환 이동 생성자 사용 m.emplace(std::make_pair("b", "abcd")); // pair의 템플릿 생성자 사용 m.emplace("d", "ddd"); // 중복 키로 emplace m.emplace("d", "DDD"); // pair의 piecewise 생성자 사용 m.emplace(std::piecewise_construct, std::forward_as_tuple("c"), std::forward_as_tuple(10, 'c')); for (const auto& p : m) std::cout << p.first << " => " << p.second << '\n'; }
출력:
a => a b => abcd c => cccccccccc d => ddd d => DDD
참고 항목
|
힌트를 사용하여 제자리에서 요소를 생성합니다
(public member function) |
|
|
요소를 삽입합니다
(public member function) |