名前空間
変種
操作

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

提供: cppreference.com
 
 
 
 
unordered_set& operator=( const unordered_set& other );
(1) (C++11以上)
(2)
unordered_set& operator=( unordered_set&& other );
(C++11以上)
(C++17未満)
unordered_set& operator=( unordered_set&& other ) noexcept(/* see below */);
(C++17以上)
unordered_set& operator=( std::initializer_list<value_type> ilist );
(3) (C++11以上)

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

1) コピー代入演算子。 内容を other の内容のコピーで置き換えます。 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::valuetrue の場合、ターゲットのアロケータはソースのアロケータのコピーで置き換えられます。 ターゲットとソースのアロケータを比較して等しくない場合、ターゲット (*this) のアロケータがメモリを解放するための使用され、その後、要素をコピーする前に、 other のアロケータがメモリを確保するために使用されます。 (C++11以上)
2) ムーブ代入演算子。 内容をムーブセマンティクスを用いて other の内容で置き換えます (つまり other のデータが other からこのコンテナにムーブされます)。 処理後 other は有効ですが未規定の状態になります。 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::valuetrue の場合、ターゲットのアロケータはソースのアロケータのコピーで置き換えられます。 false の場合、ソースとターゲットのアロケータを比較して等しくなければ、ターゲットはソースのメモリの所有権を取得することができず、必要に応じて自身のアロケータを使用して追加のメモリを確保し、個々の要素を個別にムーブ代入しなければなりません。 いずれの場合でも、元々 *this に格納されていたすべての要素は、破壊されるか、要素単位のムーブ代入によって置き換えられます。
3) 内容を初期化子リスト ilist によって表された内容で置き換えます。

目次

[編集] 引数

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

[編集] 戻り値

*this

[編集] 計算量

1) *thisother のサイズに比例。
2) アロケータが比較して等しくなく、伝播しない場合、 *thisother のサイズに比例。 そうでなければ、 *this のサイズに比例。
3) *thisilist のサイズに比例。

例外

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.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG issue 2321 で検討されています。

[編集]

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

#include <unordered_set>
#include <iostream>
 
void display_sizes(const std::unordered_set<int> &nums1,
                   const std::unordered_set<int> &nums2,
                   const std::unordered_set<int> &nums3)
{
    std::cout << "nums1: " << nums1.size() 
              << " nums2: " << nums2.size()
              << " nums3: " << nums3.size() << '\n';
}
 
int main()
{
    std::unordered_set<int> nums1 {3, 1, 4, 6, 5, 9};
    std::unordered_set<int> nums2; 
    std::unordered_set<int> nums3;
 
    std::cout << "Initially:\n";
    display_sizes(nums1, nums2, nums3);
 
    // copy assignment copies data from nums1 to nums2
    nums2 = nums1;
 
    std::cout << "After assigment:\n"; 
    display_sizes(nums1, nums2, nums3);
 
    // move assignment moves data from nums1 to nums3,
    // modifying both nums1 and nums3
    nums3 = std::move(nums1);
 
    std::cout << "After move assigment:\n"; 
    display_sizes(nums1, nums2, nums3);
}

出力:

Initially:
nums1: 6 nums2: 0 nums3: 0
After assigment:
nums1: 6 nums2: 6 nums3: 0
After move assigment:
nums1: 0 nums2: 6 nums3: 6

[編集] 関連項目

unordered_set を構築します
(パブリックメンバ関数) [edit]