GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::unordered_multiset::merge - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. unordered_set
  3. unordered_multiset
  4. merge

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<unordered_set>

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

template<class H2, class P2>
void merge(unordered_set<Key, H2, P2, Allocator>& source);           // (1) C++17
template<class H2, class P2>
constexpr void merge(unordered_set<Key, H2, P2, Allocator>& source); // (1) C++26

template<class H2, class P2>
void merge(unordered_set<Key, H2, P2, Allocator>&& source);           // (2) C++17
template<class H2, class P2>
constexpr void merge(unordered_set<Key, H2, P2, Allocator>&& source); // (2) C++26

template<class H2, class P2>
void merge(unordered_multiset<Key, H2, P2, Allocator>& source);           // (3) C++17
template<class H2, class P2>
constexpr void merge(unordered_multiset<Key, H2, P2, Allocator>& source); // (3) C++26

template<class H2, class P2>
void merge(unordered_multiset<Key, H2, P2, Allocator>&& source);           // (4) C++17
template<class H2, class P2>
constexpr void merge(unordered_multiset<Key, H2, P2, Allocator>&& source); // (4) C++26

概要

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

要件

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

戻り値

なし

計算量

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

例外

max_load_factor()の不変条件を維持するために再ハッシュが必要となる場合があり、その際のメモリ確保により例外が送出される可能性がある。また、ハッシュ関数やキー等価述語が例外を送出する場合もある。

備考

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

例

#include <iostream>
#include <unordered_set>

int main()
{
  std::unordered_multiset<int> s1 = { 10, 20, 30 };
  std::unordered_multiset<int> s2 = { 10 };

  // s1 の要素を s2 に merge
  s2.merge(s1);

  if (s1.size() != 0) std::cout << "s1 = { ";
  else std::cout << "s1 = {}\n";

  for(auto&& itr = s1.begin(); itr != s1.end();)
    std::cout << *itr << (++itr != s1.end() ? ", " : " }\n");

  if (s2.size() != 0) std::cout << "s2 = { ";
  else std::cout << "s2 = {}\n";

  for(auto&& itr = s2.begin(); itr != s2.end();)
    std::cout << *itr << (++itr != s2.end() ? ", " : " }\n");
}

出力例

s1 = {}
s2 = { 10, 10, 20, 30 }

バージョン

言語

  • C++17

処理系

  • Clang: 7.0.0 ✅
  • GCC: 7.1.0 ✅
  • Visual C++: 2017 Update 5 ✅

参照

  • Splicing Maps and Sets(Revision 5)
  • LWG Issue 2977. unordered_meow::merge() has incorrect Throws: clause
    • C++20で、「ハッシュ関数とキー等価述語以外は例外を投げない」という誤った例外指定が削除された
    • この修正は欠陥報告(DR)であり、C++17に遡及して適用される。max_load_factorの不変条件を維持するための再ハッシュでメモリ確保が発生しうるため、元の「投げない」保証は実装不可能だったため
  • P3372R3 constexpr containers and adaptors
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.