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

来自cppreference.com
< cpp‎ | container‎ | flat map
 
 
 
 
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}

[编辑]参阅

提取底层容器
(公开成员函数)[编辑]

close