public member function
<array>

std::array::swap

void swap (array& x) noexcept(noexcept(swap(declval<value_type&>(),declval<value_type&>())));
Swap content
Exchanges the content of the array by the content of x, which is another array object of the same type (including the same size).

After the call to this member function, the elements in this container are those which were in x before the call, and the elements of x are those which were in this.

Unlike with the swap member functions of the other containers, this member function operates in linear time by performing as many individual swap operations between the individual elements as their size (see swap).

Parameters

x
Another array container of the same type (which includes same size) as this whose content is swapped with that of this container.

Return value

none.

This member function can throw an exception if one of the element-wise swap calls throws itself an exception.

Example

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

int main ()
{
  std::array<int,5> first = {10, 20, 30, 40, 50};
  std::array<int,5> second = {11, 22, 33, 44, 55};

  first.swap (second);

  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;
}


Output:
first: 11 22 33 44 55
second: 10 20 30 40 50

Complexity

Linear: Performs as many individual swap operations as the size of the arrays.

Iterator validity

The validity of all iterators, references and pointers is not changed: They remain associated with the same positions in the same container they were associated before the call, but the elements they still refer to have the swapped values.

Data races

Both the container and x are modified.
All elements in both containers are accessed by the call.

Exception safety

If the non-member specialization of swap for the type of the elements is non-throwing, the function never throws exceptions (no-throw guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).

See also

  • 9316991614 2018-02-25
  • 191841613 2018-02-25
  • 16411612 2018-02-25
  • 6081981611 2018-02-25
  • 1784561610 2018-02-25
  • 4651609 2018-02-25
  • 9348111608 2018-02-25
  • 2513771607 2018-02-25
  • 6478461606 2018-02-24
  • 4985791605 2018-02-24
  • 5637141604 2018-02-24
  • 282181603 2018-02-24
  • 6217941602 2018-02-24
  • 5076141601 2018-02-24
  • 714281600 2018-02-24
  • 6607141599 2018-02-24
  • 949041598 2018-02-24
  • 6809961597 2018-02-24
  • 671871596 2018-02-24
  • 7107821595 2018-02-24