• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <flat_map>

    std::flat_multimap::replace

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

    概要

    キーのコンテナ、値のコンテナをそれぞれ置き換える。

    効果

    flat_multimap クラスが内部で保持している containersc とすると、以下と等価。

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

    事前条件

    • key_cont.size() == mapped_cont.size() が真であること。
    • key_contkey_compare に基づいてソートされていること。

    計算量

    key_cont および mapped_cont をムーブした計算量と同じ。

    #include <algorithm>
    #include <cassert>
    #include <flat_map>
    #include <iostream>
    #include <string>
    #include <utility>
    
    int main()
    {
      std::vector<std::string> keys = {"Alice", "Bob", "Carol"};
      std::vector<int> values = {3, 1, 4};
    
      // 事前条件の確認
      assert(keys.size() == values.size());
      assert(std::is_sorted(keys.begin(), keys.end()));
    
      std::flat_multimap<std::string, int> fm;
    
      std::cout << fm.size() << std::endl;
    
      fm.replace(std::move(keys), std::move(values));
    
      std::cout << fm.size() << std::endl;
      std::cout << std::endl;
    
      std::cout << "{" << std::endl;
      for (const auto& kv: fm) {
        std::cout << "  " << kv.first << ": " << kv.second << "," << std::endl;
      }
      std::cout << "}" << std::endl;
    }
    

    出力

    0
    3
    
    {
      Alice: 3,
      Bob: 1,
      Carol: 4,
    }
    

    バージョン

    言語

    • C++23

    処理系