名前空間
変種
操作

std::set<Key,Compare,Allocator>::operator=

From cppreference.com
< cpp‎ | コンテナ‎ | set
 
 
 
 
set& operator=( const set& other );
(1)
(2)
set& operator=( set&& other );
(C++11以降)
(C++17まで)
set& operator=( set&& other ) noexcept(/* 以下参照 */);
(C++17以降)
set& operator=( std::initializer_list<value_type> ilist );
(3) (C++11以降)

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

1) コピー代入演算子。コンテナの内容を other の内容のコピーで置き換えます。

If std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value is true, the allocator of *this is replaced by a copy of other. If the allocator of *this after assignment would compare unequal to its old value, the old allocator is used to deallocate the memory, then the new allocator is used to allocate it before copying the elements. Otherwise, the memory owned by *this may be reused when possible. In any case, the elements originally belonging to *this may be either destroyed or replaced by element-wise copy-assignment.

(C++11以降)
2) ムーブ代入演算子。コンテナの内容を other の内容で、ムーブセマンティクスを使用して置き換えます(すなわち、other のデータが other からこのコンテナに移動されます)。その後、other は有効ですが未指定の状態になります。
If std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value is true, the allocator of *this is replaced by a copy of that of other. If it is false and the allocators of *this and other do not compare equal, *this cannot take ownership of the memory owned by other and must move-assign each element individually, allocating additional memory using its own allocator as needed. In any case, all elements originally belonging to *this are either destroyed or replaced by element-wise move-assignment.
3) コンテナの内容を初期化リスト ilist で指定された内容で置き換えます。

目次

[編集] パラメータ

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

[編集] 戻り値

*this

[編集] 計算量

1) *this および other のサイズに対する線形。
2) アロケータが等しくなく、かつ伝播しない場合を除き、*this のサイズに対する線形。この場合、*this および other のサイズに対する線形。
3) O(NlogN) in general, where N is size() + ilist.size(). Linear if ilist is sorted with respect to value_comp().

[編集] 例外

1-3) 実装定義の例外を投げる可能性があります。
(C++17まで)
1,3) 実装定義の例外を投げる可能性があります。
2)
noexcept 指定:  
noexcept(std::allocator_traits<Allocator>::is_always_equal::value
&& std::is_nothrow_move_assignable<Compare>::value)
(C++17以降)

[編集] 注釈

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

[編集]

The following code uses operator= to assign one std::set to another

#include <initializer_list>
#include <iostream>
#include <iterator>
#include <set>
 
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::set<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 }

[編集] 関連項目

constructs the set
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)