public member function
<forward_list>

std::forward_list::operator=

copy (1)
forward_list& operator= (const forward_list& fwdlst);
move (2)
forward_list& operator= (forward_list&& fwdlst);
initializer list(3)
forward_list& operator= (initializer_list<value_type> il);
Assign content
Assigns new contents to the container, replacing its current contents.

The copy assignment (1) copies all the elements from fwdlst into the container (with fwdlst preserving its contents).

The move assignment (2) moves the elements of fwdlst into the container (x is left in an unspecified but valid state).

The initializer list assignment (3) copies the elements of il into the container.

The container preserves its current allocator, except if the allocator traits indicate fwdlst's allocator should propagate. This allocator is used (through its traits) to allocate or deallocate if there are changes in storage requirements, and to construct or destroy elements, if needed.

Any elements held in the container before the call are either assigned to or destroyed.

引數

fwdlst
A forward_list object of the same type (i.e., with the same template parameters,TAlloc).
il
一個 initializer_list 物件。編譯器將自動從初始化列表宣告器建立此類物件。
成員型別value_typeis the type of the elements in the container, defined in forward_list as an alias of its first template parameter (T).

返回值

*this

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// assignment operator with forward_list
#include <iostream>
#include <forward_list>

template<class Container>
Container by_two (const Container& x) {
  Container temp(x); for (auto& x:temp) x*=2; return temp;
}

int main ()
{
  std::forward_list<int> first (4);      // 4 ints
  std::forward_list<int> second (3,5);   // 3 ints with value 5

  first = second;                        // copy assignment
  second = by_two(first);                // move assignment

  std::cout << "first: ";
  for (int& x : first) std::cout << ' ' << x;
  std::cout << '\n';

  std::cout << "second: ";
  for (int& x : second) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}
In the first assignment,secondis an lvalue: the copy assignment function is called.
In the second assignment, the value returned byby_two(first)is an rvalue: the move assignment function is called.
輸出
first: 5 5 5
second: 10 10 10


複雜度

Linear in the number of elements.

迭代器有效性

All iterators, references and pointers related to this container are invalidated, except the end iterators.

In the move assignment, iterators, pointers and references referring to elements in x are also invalidated.

資料競爭

All copied elements are accessed.
The move assignment (2) modifies fwdlst.
The container and all its elements are modified.

異常安全

Basic guarantee: if an exception is thrown, the container is in a valid state.
If allocator_traits::construct is not supported with the appropriate arguments for the element constructions, or ifvalue_typeis not copy assignable (or move assignable for (2)), it causes undefined behavior.

另見