public member function
<deque>

std::deque::swap

void swap (deque& x);
交换内容
Exchange the contents of the container with those of x, which is another deque container of the same type. Sizes may differ.

After calling this member function, the elements in this container are those that were in x before the call, and the elements of x are those that were in this. All iterators, references, and pointers remain valid for the swapped objects.

Note that a non-member function with the same name, swap, also exists, overloading this algorithm with an optimization that behaves like this member function.

No specifics on allocators. [contradictory specifications]
Whether the container allocators are also swapped is not defined, unless the appropriate allocator trait explicitly indicates that they shall propagate.

参数

x
Another deque container of the same type (i.e., instantiated with the same template parameters,TAlloc)其内容与此容器的内容交换。

返回值



示例

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

main ()
{
  unsigned int i;
  std::deque<int> foo (3,100);   // three ints with a value of 100
  std::deque<int> bar (5,200);   // five ints with a value of 200

  foo.swap(bar);

  std::cout << "foo contains:";
  for (std::deque<int>::iterator it = foo.begin(); it!=foo.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  std::cout << "bar contains:";
  for (std::deque<int>::iterator it = bar.begin(); it!=bar.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

输出
foo contains: 200 200 200 200 200 
bar contains: 100 100 100 


复杂度

常量。

迭代器有效性

All iterators, pointers, and references referring to elements in both containers remain valid and now refer to the same elements they referred to before the call, but in the other container, where they now iterate.
Note that the end iterators do not refer to elements and may be invalidated.

数据竞争

容器和 x 都被修改。
No contained elements are accessed by the call (although see iterator validity above).

异常安全

如果两个容器中的分配器进行比较相等,或者它们的 分配器特性 表明分配器应该 传播,则该函数永远不会抛出异常(无异常保证)。
否则,将导致未定义行为

另见