public member function
<valarray>

std::valarray::operator=

copy (1)
valarray& operator=(const valarray& x);
fill (2)
valarray& operator=(const T& val);
sub-array (3)
valarray& operator=(const slice_array<T>& sub);
valarray& operator=(const gslice_array<T>& sub);
valarray& operator=(const mask_array<T>& sub);
valarray& operator=(const indirect_array<T>& sub);
copy/move (1)
valarray& operator=(const valarray& x);
valarray& operator=(valarray&& x) noexcept;
fill (2)
valarray& operator=(const T& val);
sub-array (3)
valarray& operator=(const slice_array<T>& sub);
valarray& operator=(const gslice_array<T>& sub);
valarray& operator=(const mask_array<T>& sub);
valarray& operator=(const indirect_array<T>& sub);
Assign content
Assigns contents to the valarray object:

(1) copy assignment
Assigns the contents of x to *this: each element is assigned the value of the corresponding element in x.
If the sizes do not match, the behavior is not defined.
(2) fill assignment
Assigns val to every element.
(3) sub-array assignment
Assigns to each element the value of the corresponding element in sub (sizes shall match).
(1) copy / move assignment
Assigns the contents of x to *this after resizing the object (if necessary):
- The copy assignment assigns to each element the value of the corresponding element in x.
- The move assignment acquires the contents of x, leaving x in an unspecified but valid state.
(2) fill assignment
Assigns val to every element.
The size of the valarray is preserved, with each of its element equal to this value.
(3) sub-array assignment
Assigns to each element the value of the corresponding element in sub (sizes shall match).

Parameters

x
A valarray object of the same type (with the same class template argument T).
val
Value assigned to all the elements are in the valarray.
T is the template argument of valarray (the value type).
sub
The result of a valarray subscripting operation.
T is the template argument of valarray (the value type).

Return value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// valarray assignment example
#include <iostream>     // std::cout
#include <valarray>     // std::valarray, std::slice

int main ()
{                                //    foo:      bar:

  std::valarray<int> foo (4);    //  0 0 0 0
  std::valarray<int> bar (2,4);  //  0 0 0 0   2 2 2 2

  foo = bar;                     //  2 2 2 2   2 2 2 2
  bar = 5;                       //  2 2 2 2   5 5 5 5
  foo = bar[std::slice (0,4,1)]; //  5 5 5 5   5 5 5 5

  std::cout << "foo sums " << foo.sum() << '\n';

  return 0;
}


Output:

foo sums 20

Complexity

Depends on library implementation (operations may be parallelized).

Iterator validity

No changes: Valid iterators, references and sub-arrays keep their validity.
For copy/move assignment (1) when the sizes do not match: Invalidates all iterators, references and sub-arrays to elements of the valarray.
In all other cases: Valid iterators, references and sub-arrays keep their validity.

Data races

All copied elements are accessed.
The move assignment modifies x.
The object and all its elements are modified.

Exception safety

If any operation performed on the elements throws an exception, it causes undefined behavior.
If the function needs to allocate storage and fails, it may throw an exception (such as bad_alloc), although this is not mandated.
The move assignment never throws exceptions (no-throw guarantee).

See also