名前空間
変種
操作

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

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

[編集] 注釈

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

[編集]

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

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <forward_list>
 
void print(auto const comment, auto const& container)
{
    auto size = std::ranges::distance(container);
    std::cout << comment << "{ ";
    for (auto const& element : container)
        std::cout << element << (--size ? ", " : " ");
    std::cout << "}\n";
}
 
int main()
{
    std::forward_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 }

[編集] 関連項目

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