名稱空間
變體
操作

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

來自 cppreference.com
 
 
 
 
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已排序。

    否則,行為未定義。

目錄

[edit] 引數

keys_cont - 型別為KeyContainer的已排序鍵容器,其內容將被移動到*this
mapped_cont - 型別為MappedContainer的對映值容器,其內容將被移動到*this

[edit] 返回值

(無)

[edit] 複雜度

等於應用於適配容器的std::move的複雜度。

[edit] 示例

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

[edit] 參閱

提取底層容器
(公共成員函式) [編輯]