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

履歴 編集

function template
<unordered_map>

std::unordered_multimap::merge(C++17)

template<class H2, class P2>
  void merge(unordered_map<Key, T, H2, P2, Allocator>& source);       // (1)
template<class H2, class P2>
  void merge(unordered_map<Key, T, H2, P2, Allocator>&& source);      // (2)
template<class H2, class P2>
  void merge(unordered_multimap<Key, T, H2, P2, Allocator>& source);  // (3)
template<class H2, class P2>
  void merge(unordered_multimap<Key, T, H2, P2, Allocator>&& source); // (4)

概要

引数 source 内の各要素を抽出し、*this のハッシュ関数とキー等価(equality)述語を使用して *this への挿入を試みる。 また、この操作で要素はコピーもムーブもされない。

要件

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

戻り値

なし

計算量

平均的なケースでは O(N)、最悪ケースでは O(N*size()+N)、ただし Nsource.size() である。

備考

source の転送された要素へのポインタおよび参照は、それらと同じ要素を参照するが、*this のメンバとして参照する。また、転送された要素を参照する反復子と*thisを参照するすべての反復子は無効になるが、 source に残っている要素への反復子は有効なままになる。

#include <iostream>
#include <unordered_map>

int main()
{
  std::unordered_multimap<int, char> m1 = {
    {10, 'a'},
    {20, 'b'},
    {30, 'c'}
  };
  std::unordered_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

処理系

参照