public member function
<valarray>

std::valarray::swap

void swap (valarray& x) noexcept;
Swap valarray contents
Exchanges the contents of *this and x.

This operation is performed in constant time.

Parameters

x
Another valarray of the same type (with the same template argument, T). Sizes may differ.

Return value

none

Example

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

int main ()
{
  std::valarray<int> foo {10,20,30,40};
  std::valarray<int> bar {100,200,300};

  foo.swap(bar);

  std::cout << "foo contains:";
  for (auto& x: foo) std::cout << ' ' << x;
  std::cout << '\n';

  std::cout << "bar contains:";
  for (auto& x: bar) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:

foo contains 100 200 300
bar contains 10 20 30 40


Complexity

Constant.

Iterator validity

All valid iterators, references and sub-arrays of both x and *this keep their validity, and are now referring to the same elements they referred to before the call, but in the other valarray.
Note that the end iterators do not refer to elements and may be invalidated.

Data races

Both the object and x are modified.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also