函式模板
<deque>

std::swap (deque)

template <class T, class Alloc>  void swap (deque<T,Alloc>& x, deque<T,Alloc>& y);
交換兩個 deque 容器的內容
容器 x 的內容與 y 的內容進行交換。兩個容器物件必須是相同的型別(具有相同的模板引數),儘管它們的大小可能不同。

呼叫此成員函式後,x 中的元素將是呼叫前 y 中的元素,而 y 中的元素將是呼叫前 x 中的元素。所有迭代器、引用和指標對於交換後的物件仍然有效。

這是泛型演算法 swap 的一個過載,它透過相互轉移它們資產的所有權來提高其效能(即,容器交換它們資料的引用,而無需實際進行任何元素複製或移動):它的行為相當於x.swap(y)被呼叫。

引數

x,y
相同型別的 deque 容器(即,具有相同的模板引數,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 (deque overload)
#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

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


複雜度

常量。

迭代器有效性

指向兩個容器中元素的的所有迭代器、指標和引用仍然有效,並且現在指向呼叫之前它們所指向的相同元素,但在另一個容器中,它們現在可以進行迭代。
請注意,結束迭代器 不指向元素,可能會失效。

資料競爭

容器 xy 都將被修改。

異常安全

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

另見