名前空間
変種
操作

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

From cppreference.com
 
 
 
 
unordered_multimap& operator=( const unordered_multimap& other );
(1) (C++11以降)
(2)
unordered_multimap& operator=( unordered_multimap&& other );
(C++11以降)
(C++17まで)
unordered_multimap& operator=( unordered_multimap&& other ) noexcept(/* 後述 */);
(C++17以降)
unordered_multimap& 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 のコピーで置き換えられます。 *this の代入後のアロケータが元の値と等しくない比較になる場合、古いアロケータはメモリを解放するために使用され、その後新しいアロケータが要素のコピーに使用されます。それ以外の場合、 *this が所有するメモリは可能な限り再利用されることがあります。いずれの場合も、 *this が元々所有していた要素は破棄されるか、要素ごとのコピー代入で置き換えられる可能性があります。
2) ムーブ代入演算子。コンテナの内容を other の内容で、ムーブセマンティクスを使用して置き換えます(すなわち、other のデータが other からこのコンテナに移動されます)。その後、other は有効ですが未指定の状態になります。
std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::valuetrue の場合、 *this のアロケータは other のアロケータのコピーで置き換えられます。 false であり、 *thisother のアロケータが等しく比較されない場合、 *thisother が所有するメモリを引き継ぐことができず、自身の allocator を使用して追加のメモリを必要に応じて確保しながら、各要素を個別に移動代入する必要があります。いずれの場合も、 *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 を通じてより直接的な保証が検討されています。

[編集]

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

#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_multimap<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_multimap を構築します。
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)