public member function
<stack>

std::stack::swap

void swap (stack& x) noexcept(/*see below*/);
Swap contents
Exchanges the contents of the container adaptor (*this) by those of x.

This member function calls the non-member function swap (unqualified) to swap the underlying containers.

The noexcept specifier matches the swap operation on the underlying container.

Parameters

x
Another stack container adaptor of the same type (i.e., instantiated with the same template parameters, T and Container). Sizes may differ.

Return value

none

Example

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;
}


Output:

size of foo: 2
size of bar: 3

Complexity

Constant.

Data races

Both *this and x are modified.

Exception safety

Provides the same level of guarantees as the operation performed on the underlying container objects.

See also