目录

swap

描述 (Description)

C ++函数std::queue::swap(queue)交换两个队列的内容。

声明 (Declaration)

以下是std :: queue :: swap(queue)函数形式std :: queue头的声明。

C++11

template <class T, class Container>
void swap (queue<T,Container>& q1, queue<T,Container>& q2) noexcept;

参数 (Parameters)

  • q1 - 第一个队列对象。

  • q2 - 第二个队列对象。

返回值

没有

异常 (Exceptions)

该成员函数从不抛出异常。

时间复杂

线性即O(n)

例子 (Example)

以下示例显示了std :: queue :: swap(queue)函数的用法。

#include <iostream>
#include <queue>
using namespace std;
int main(void) {
   queue<int> q1, q2;
   for (int i = 0; i < 5; ++i)
      q1.push(i + 1);
   for (int i = 0; i < 3; ++i)
      q2.push(i + 100);
   swap(q1, q2);
   cout << "Contents of q1 and q2 after swap operation" << endl;
   while (!q1.empty()) {
      cout << q1.front() << endl;
      q1.pop();
   }
   cout << endl << endl;
   while (!q2.empty()) {
      cout << q2.front() << endl;
      q2.pop();
   }
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Contents of q1 and q2 after swap operation
100
101
102
1
2
3
4
5
↑回到顶部↑
WIKI教程 @2018