A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/valarray/valarray/swap-free/ below:

function template

<valarray>

std::swap (valarray)
template <class T>  void swap (valarray<T>& x, valarray<T>& y) noexcept;

Swap valarrays

Exchanges the contents of x and y.

This is an overload of swap that behaves as if x.swap(y) was called, operating in constant time.



Parameters
x,y
valarray objects of the same type to swap. 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
// swap valarrays
#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};

  swap(foo,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 y 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.

Complexity Constant.

Data races Both objects, x and y, are modified.

Exception safetyNo-throw guarantee: never throws exceptions.

See also
valarray::swap
Swap valarray contents (public member function)
valarray::operator=
Assign content (public member function)
swap_ranges
Exchange values of two ranges (function template)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4