名前空間
変種
操作

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: operator=

From cppreference.com
 
 
 
 
unordered_map& operator=( const unordered_map& other );
(1) (C++11以降)
(2)
unordered_map& operator=( unordered_map&& other );
(C++11以降)
(C++17まで)
unordered_map& operator=( unordered_map&& other ) noexcept(/* see below */);
(C++17以降)
unordered_map& operator=( std::initializer_list<value_type> ilist );
(3) (C++11以降)

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

1) コピー代入演算子。コンテナの内容を other の内容のコピーで置き換えます。
もし std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value が true ならば、*this のアロケータは other のコピーで置き換えられます。*this の代入後のアロケータが古い値と等しくない場合、古いアロケータがメモリを解放し、次に新しいアロケータが要素をコピーするために使用されます。そうでなければ、*this が所有するメモリは、可能な場合は再利用されることがあります。いずれの場合も、*this が元々所有していた要素は破棄されるか、要素ごとのコピー代入で置き換えられる可能性があります。
2) ムーブ代入演算子。コンテナの内容を other の内容で、ムーブセマンティクスを使用して置き換えます(すなわち、other のデータが other からこのコンテナに移動されます)。その後、other は有効ですが未指定の状態になります。
もし std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value が true ならば、*this のアロケータは other のアロケータのコピーで置き換えられます。false で、*this と other のアロケータが等しくない場合、*this は other が所有するメモリを引き継ぐことができず、必要に応じて自身のメモリを使用して各要素を移動代入しなければなりません。いずれの場合も、*this が元々所有していたすべての要素は破棄されるか、要素ごとの移動代入で置き換えられる可能性があります。
3) コンテナの内容を初期化リスト ilist で指定された内容で置き換えます。

目次

[編集] パラメータ

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

[編集] 戻り値

*this

[編集] 計算量

1) *this および other のサイズに対する線形。
2) アロケータが等しくなく、かつ伝播しない場合を除き、*this のサイズに対する線形。この場合、*this および other のサイズに対する線形。
3) *this および ilist のサイズに対する線形。

[編集] 例外

1-3) 実装定義の例外を投げる可能性があります。
(C++17まで)
1,3) 実装定義の例外を投げる可能性があります。
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 を通じてより直接的な保証が検討されています。

[編集]

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

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <unordered_map>
#include <utility>
 
void print(auto const comment, auto const& container)
{
    auto size = std::size(container);
    std::cout << comment << "{ ";
    for (auto const& [key, value] : container)
        std::cout << '{' << key << ',' << value << (--size ? "}, " : "} ");
    std::cout << "}\n";
}
 
int main()
{
    std::unordered_map<int, int> x{{1,1}, {2,2}, {3,3}}, y, z;
    const auto w = {std::pair<const int, int>{4,4}, {5,5}, {6,6}, {7,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,3}, {2,2}, {1,1} }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { {3,3}, {2,2}, {1,1} }
y = { {3,3}, {2,2}, {1,1} }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { {3,3}, {2,2}, {1,1} }
Assignment of initializer_list w to z:
w = { {4,4}, {5,5}, {6,6}, {7,7} }
z = { {7,7}, {6,6}, {5,5}, {4,4} }

[編集] 関連項目

unordered_map を構築します。
(公開メンバ関数) [編集]
"https://ja.cppreference.dev/mwiki/index.php?title=cpp/container/unordered_map/operator%3D&oldid=136026" から取得
English 日本語 中文(简体) 中文(繁體)