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

異常安全

如果兩個容器中的分配器進行比較相等,或者它們的 分配器特性 表明分配器應該 傳播,則該函式永遠不會丟擲異常(無異常保證)。
否則,將導致未定義行為

另見