Namespaces
Variants

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

From cppreference.net

void replace ( key_container_type && key_cont, mapped_container_type && mapped_cont ) ;
(C++23 이후)

기반 컨테이너 c 를 교체합니다. 다음 코드와 동일합니다:

c.keys = std::move(key_cont);
c.values = std::move(mapped_cont);

다음 조건이 충족되어야 합니다:

  • 다음 표현식이 key_cont. size ( ) == mapped_cont. size ( ) true 여야 하고,
  • key_cont 의 요소들이 compare 에 따라 정렬되어야 하며,
  • key_cont 가 동일한 요소를 포함하지 않아야 합니다.

    그렇지 않으면 동작이 정의되지 않습니다.

목차

매개변수

keys_cont - KeyContainer 타입의 정렬된 키 컨테이너로, 그 내용이 * this 로 이동됩니다
mapped_cont - MappedContainer 타입의 매핑된 값 컨테이너로, 그 내용이 * this 로 이동됩니다

반환값

(없음)

복잡도

적응된 컨테이너들에 적용된 std::move 의 복잡도와 동일합니다.

예제

#include <algorithm>
#include <cassert>
#include <flat_map>
#include <print>
#include <vector>
int main()
{
    std::vector<int> keys{1, 2, 3};
    assert(std::ranges::is_sorted(keys));
    std::vector<double> values{2.2, 3.3, 1.1};
    assert(keys.size() == values.size());
    std::flat_map<int, double> map;
    assert(map.empty());
    map.replace(keys, values);
    assert(map.size() == 3);
    assert(map.keys() == 3);
    assert(map.values() == 3);
    assert(keys.empty());
    assert(values.empty());
    std::println("{}", map);
}

출력:

{1: 2.2, 2: 3.3, 3: 1.1}

참고 항목

기본 컨테이너를 추출합니다
(public member function)