std::forward_list<T,Allocator>::operator=
提供: cppreference.com
< cpp | container | forward list
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(/* see below */); |
(C++17以上) | |
forward_list& operator=( std::initializer_list<T> ilist ); |
(3) | (C++11以上) |
コンテナの内容を置き換えます。
1) コピー代入演算子。 内容を
other
の内容のコピーで置き換えます。 std::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value が true の場合、ターゲットのアロケータはソースのアロケータのコピーで置き換えられます。 ターゲットとソースのアロケータを比較して等しくない場合、ターゲット (*this) のアロケータがメモリを解放するための使用され、その後、要素をコピーする前に、 other
のアロケータがメモリを確保するために使用されます。 (C++11以上)2) ムーブ代入演算子。 内容をムーブセマンティクスを用いて
other
の内容で置き換えます (つまり other
のデータが other
からこのコンテナにムーブされます)。 処理後 other
は有効ですが未規定の状態になります。 std::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value が true の場合、ターゲットのアロケータはソースのアロケータのコピーで置き換えられます。 false の場合、ソースとターゲットのアロケータを比較して等しくなければ、ターゲットはソースのメモリの所有権を取得することができず、必要に応じて自身のアロケータを使用して追加のメモリを確保し、個々の要素を個別にムーブ代入しなければなりません。 いずれの場合でも、元々 *this に格納されていたすべての要素は、破壊されるか、要素単位のムーブ代入によって置き換えられます。3) 内容を初期化子リスト
ilist
によって表された内容で置き換えます。目次 |
[編集] 引数
other | - | データソースとして使用される別のコンテナ |
ilist | - | データソースとして使用される初期化子リスト |
[編集] 戻り値
*this。
[編集] 計算量
1)
*this
と other
のサイズに比例。2) アロケータが比較して等しくなく、伝播しない場合、
*this
と other
のサイズに比例。 そうでなければ、 *this
のサイズに比例。3)
*this
と ilist
のサイズに比例。
例外2) noexcept 指定:
noexcept(std::allocator_traits<Allocator>::is_always_equal::value) |
(C++17以上) |
[編集] ノート
コンテナのムーブ代入 (オーバーロード (2)) の後、アロケータの非互換によって要素単位のムーブ代入が強制されない限り、 other
を指す参照、ポインタ、イテレータ (終端イテレータを除く) は有効なままですが、 *this 内の要素を参照するようになります。 現行の標準ではこの保証は [container.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG issue 2321 で検討されています。
[編集] 例
以下のコードは operator=
を使用して std::forward_list を別の std::forward_list に代入します。
Run this code
#include <forward_list> #include <iostream> void display_sizes(const std::forward_list<int> &nums1, const std::forward_list<int> &nums2, const std::forward_list<int> &nums3) { std::cout << "nums1: " << std::distance(nums1.begin(), nums1.end()) << " nums2: " << std::distance(nums2.begin(), nums2.end()) << " nums3: " << std::distance(nums3.begin(), nums3.end()) << '\n'; } int main() { std::forward_list<int> nums1 {3, 1, 4, 6, 5, 9}; std::forward_list<int> nums2; std::forward_list<int> nums3; std::cout << "Initially:\n"; display_sizes(nums1, nums2, nums3); // copy assignment copies data from nums1 to nums2 nums2 = nums1; std::cout << "After assigment:\n"; display_sizes(nums1, nums2, nums3); // move assignment moves data from nums1 to nums3, // modifying both nums1 and nums3 nums3 = std::move(nums1); std::cout << "After move assigment:\n"; display_sizes(nums1, nums2, nums3); }
出力:
Initially: nums1: 6 nums2: 0 nums3: 0 After assigment: nums1: 6 nums2: 6 nums3: 0 After move assigment: nums1: 0 nums2: 6 nums3: 6
[編集] 関連項目
forward_list を構築します (パブリックメンバ関数) | |
コンテナに値を代入します (パブリックメンバ関数) |