• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <map>

    std::multimap::merge

    template<class C2>
      void merge(map<Key, T, C2, Allocator>& source);       // (1)
    template<class C2>
      void merge(map<Key, T, C2, Allocator>&& source);      // (2)
    template<class C2>
      void merge(multimap<Key, T, C2, Allocator>& source);  // (3)
    template<class C2>
      void merge(multimap<Key, T, C2, Allocator>&& source); // (4)
    

    概要

    引数 source 内の各要素を抽出し、*this の比較オブジェクトを使用して *this への挿入を試みる。
    また、この操作で要素はコピーもムーブもされない。

    要件

    source.get_allocator() == (*this).get_allocator()

    戻り値

    なし

    計算量

    Nlog((*this).size()+N)
    ここで、N の値は source.size() である。

    備考

    • source の転送された要素へのポインタおよび参照は、それらと同じ要素を参照するが、*this のメンバとして参照する。また、転送された要素を参照する反復子は、引き続きその要素を参照するが、転送後は source ではなく *this への反復子として動作する
    • (2), (4) : これらの右辺値参照オーバーロードは、一時オブジェクトを受け取った場合にコピーを発生させないためだけのものである。パラメータのオブジェクト内のポインタを破壊したり、高速なmerge処理が行われるわけではない

    例外

    比較オブジェクトが例外をスローしない限り、例外をスローしない。

    #include <iostream>
    #include <map>
    
    int main()
    {
      std::multimap<int, char> m1 = {
        {10, 'a'},
        {20, 'b'},
        {30, 'c'}
      };
      std::multimap<int, char> m2 = {
        {10, 'x'}
      };
    
      // m1 の要素を m2 に merge
      m2.merge(m1);
    
      std::cout << "m1 :" << std::endl;
      for (const auto& [key, value] : m1)
        std::cout << "[" << key << ", " << value << "]" << std::endl;
    
      std::cout << "\n" << "m2 :" << std::endl;
      for (const auto& [key, value] : m2)
        std::cout << "[" << key << ", " << value << "]" << std::endl;
    }
    

    出力

    m1 :
    
    m2 :
    [10, x]
    [10, a]
    [20, b]
    [30, c]
    

    バージョン

    言語

    • C++17

    処理系

    参照