STLarray的swap方法(19)


STLarray的swap方法(19)

原文地址:http://www.cplusplus.com/reference/array/array/swap/
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).

与x交换内容,x是一个同类型的array(包括大小)。


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 are those which were in this.

调用该函数之后,存储在该容器内的元素是那些本来在x中的元素,x中的元素是原本存储在该容器中的元素(即交换内容)。


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).

和其他容器的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.

x是同一类型的array(大小也一样),将要和本array交换内容的array.



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 contains: 11 22 33 44 55
second contains: 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).

如果调用非成员方法swap在该类型的元素不会抛出异常,那么该方法也不会抛出异常。

Otherwise, the container is guaranteed to end in a valid state (basic guarantee).

否则,容器保证在有效的状态。



——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-8-30

于GDUT

——————————————————————————————————————————————————————————————————



发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注