名前空間
変種
操作

std::list<T,Allocator>::operator=

From cppreference.com
< cpp‎ | container‎ | list
 
 
 
 
list& operator=( const list& other );
(1)
(2)
list& operator=( list&& other );
(C++11以降)
(C++17まで)
list& operator=( list&& other ) noexcept(/* 以下参照 */);
(C++17以降)
list& 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 が元々所有していた要素は、破棄されるか、要素ごとのコピー代入で置き換えられることがあります。

(C++11以降)
2) ムーブ代入演算子。コンテナの内容を other の内容で、ムーブセマンティクスを使用して置き換えます(すなわち、other のデータが other からこのコンテナに移動されます)。その後、other は有効ですが未指定の状態になります。
もし std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::valuetrue ならば、*this のアロケータは `other` のアロケータのコピーで置き換えられます。もし false であり、*thisother のアロケータが等しく比較されない場合、*this は `other` が所有するメモリを引き継ぐことができず、必要に応じて自身の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)
(C++17以降)

[編集] 注釈

コンテナのムーブ代入後(オーバーロード (2))、互換性のないアロケータによる要素ごとのムーブ代入が強制されない限り、other への参照、ポインタ、およびイテレータ(終端イテレータを除く)は有効なままですが、*this に含まれるようになった要素を参照します。現在の標準では、[container.reqmts]/67 の包括的な声明によりこの保証が提供されており、LWG issue 2321 を通じてより直接的な保証が検討されています。

[編集]

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

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

[編集] 関連項目

list を構築します
(公開メンバ関数) [編集]
コンテナに値を代入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)