C++官网参考链接:https://cplusplus.com/reference/queue/queue/swap/
公有成员函数
<queue>
std::queue::swap
void swap (queue& x) noexcept(/*see below*/);
交换内容
将容器适配器(*this)的内容与x的内容交换。
该成员函数调用非成员函数swap(非限定的)来交换底层容器。
noexcept说明符匹配底层容器上的swap操作。
形参
x
另一个相同类型的queue容器适配器(即,使用相同的模板形参T和Container实例化)。大小可能不同。
返回值
没有返回值。
用例
// queue::swap
#include <iostream> // std::cout
#include <queue> // std::queue
int main ()
{
std::queue<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
复杂度
常量。
数据竞争
*this和x都被修改了。
异常安全
提供与在底层容器对象上执行的操作相同级别的保证。