目录

queue::swap

描述 (Description)

C ++函数std::queue::swap()用另一个队列x的内容交换队列的内容。

声明 (Declaration)

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

C++11

void swap (queue& x) noexcept;

参数 (Parameters)

x - 相同类型的另一个队列对象。

返回值

没有

时间复杂

线性即O(n)

例子 (Example)

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

#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 < 2; ++i)
      q2.push(i + 150);
   q1.swap(q2);
   cout << "Content 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;
}

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

Content of q1 and q2 after swap operation
150
151
1
2
3
4
5
↑回到顶部↑
WIKI教程 @2018