Namespaces
Variants

std::unordered_set<Key,Hash,KeyEqual,Allocator>:: operator=

From ja.cppreference.net

unordered_set & operator = ( const unordered_set & other ) ;
(1) (C++11以降)
(C++26以降 constexpr)
(2)
unordered_set & operator = ( unordered_set && other ) ;
(C++11以降)
(C++17まで)
unordered_set & operator = ( unordered_set && other )
noexcept ( /* see below */ ) ;
(C++17以降)
(C++26以降 constexpr)
unordered_set & operator = ( std:: initializer_list < value_type > ilist ) ;
(3) (C++11以降)
(C++26以降 constexpr)

コンテナの内容を置き換えます。

traits を std:: allocator_traits < allocator_type > とします:

1) コピー代入演算子。内容を other の内容のコピーで置き換えます。
traits::propagate_on_container_copy_assignment::valueが true の場合、 * this のアロケータは other のコピーで置き換えられる。代入後の * this のアロケータが古い値と等しく比較されない場合、古いアロケータを使用してメモリを解放した後、新しいアロケータを使用して要素をコピーする前にメモリを確保する。それ以外の場合、 * this が所有するメモリは可能な場合に再利用される。いずれの場合も、元々 * this に属していた要素は、破棄されるか要素ごとのコピー代入で置き換えられる可能性がある。
2) ムーブ代入演算子。内容を other の内容で置き換える(ムーブセマンティクスを使用、すなわち other のデータが other からこのコンテナにムーブされる)。 other はその後、有効だが未規定の状態となる。
traits :: propagate_on_container_move_assignment :: value が true の場合、 * this のアロケータは other のアロケータのコピーで置き換えられる。 false であり、かつ * this と other のアロケータが等しくない場合、 * this は other が所有するメモリの所有権を取得できず、各要素を個別にムーブ代入し、必要に応じて自身のアロケータを使用して追加のメモリを割り当てなければならない。いずれの場合も、元々 * this に属していたすべての要素は、破棄されるか要素ごとのムーブ代入によって置き換えられる。
3) 内容を初期化子リスト ilist で指定されたものに置き換えます。

目次

パラメータ

other - データソースとして使用する別のコンテナ
ilist - データソースとして使用する初期化子リスト

戻り値

* this

計算量

1) * this と other のサイズに対して線形。
2) アロケータが等価ではなく伝播しない場合を除き、 * this のサイズに対して線形。その場合、 * this と other のサイズに対して線形。
3) * this および ilist のサイズに対して線形。

例外

2)
noexcept 仕様:
noexcept ( std:: allocator_traits < Allocator > :: is_always_equal :: value

&& std:: is_nothrow_move_assignable < Hash > :: value

&& std:: is_nothrow_move_assignable < Pred > :: value )
(C++17以降)

注記

コンテナのムーブ代入 (オーバーロード (2)) の後、互換性のないアロケータによる要素単位のムーブ代入が強制されない限り、もともと other 内の要素を指していた参照、ポインタ、イテレータは有効なままですが、その要素は現在 *this にあります。現在の標準では、この保証は [container.reqmts]/67 における包括的な記述によって行われており、より直接的な保証は LWG issue 2321 を通じて検討中です。

例

以下のコードは operator = を使用して、ある std::unordered_set を別のものに代入します:

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <unordered_set>
void print(const auto comment, const auto& container)
{
    auto size = std::size(container);
    std::cout << comment << "{ ";
    for (const auto& element : container)
        std::cout << element << (--size ? ", " : " ");
    std::cout << "}\n";
}
int main()
{
    std::unordered_set<int> x{1, 2, 3}, y, z;
    const auto w = {4, 5, 6, 7};
    std::cout << "Initially:\n";
    print("x = ", x);
    print("y = ", y);
    print("z = ", z);
    std::cout << "Copy assignment copies data from x to y:\n";
    y = x;
    print("x = ", x);
    print("y = ", y);
    std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
    z = std::move(x);
    print("x = ", x);
    print("z = ", z);
    std::cout << "Assignment of initializer_list w to z:\n";
    z = w;
    print("w = ", w);
    print("z = ", z);
}

出力例:

Initially:
x = { 3, 2, 1 }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { 3, 2, 1 }
y = { 3, 2, 1 }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { 3, 2, 1 }
Assignment of initializer_list w to z:
w = { 4, 5, 6, 7 }
z = { 7, 6, 5, 4 }

関連項目

unordered_setを構築する
(公開メンバ関数)