Namespaces
Variants

std::flat_multimap<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 已排序。

    否则,行为未定义。

目录

参数

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_multimap<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}

参见

提取底层容器
(公开成员函数)