public member function
<stack>

std::stack::swap

void swap (stack& x) noexcept(/*see below*/);
交換內容
將容器介面卡(*this)的內容與 x 的內容交換。

此成員函式呼叫非成員函式 swap(未限定)來交換 *底層容器*。

noexcept 說明符與 *底層容器* 上的 swap 操作匹配。

引數

x
另一個相同型別的 stack 容器介面卡(即,使用相同的模板引數 TContainer 例項化)。大小可能不同。

返回值



示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// stack::swap
#include <iostream>       // std::cout
#include <stack>          // std::stack

int main ()
{
  std::stack<int> foo,bar;
  foo.push (10); foo.push(20); foo.push(30);
  bar.push (111); bar.push(222);

  foo.swap(bar);

  std::cout << "size of foo: " << foo.size() << '\n';
  std::cout << "size of bar: " << bar.size() << '\n';

  return 0;
}

輸出

size of foo: 2
size of bar: 3


複雜度

常量。

資料競爭

both *this and x are modified.

異常安全

提供與對 *底層容器* 物件執行的操作相同的保證級別。

另見