目录

priority_queue::swap

描述 (Description)

C ++函数std::priority_queue::swap()用另一个priority_queue的内容交换priority_queue的内容。

声明 (Declaration)

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

C++11

void swap (priority_queue& x) noexcept;

参数 (Parameters)

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

返回值

没有。

异常 (Exceptions)

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

时间复杂

线性即O(n)

例子 (Example)

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

#include <iostream>
#include <queue>
using namespace std;
int main(void) {
   auto it1 = {3, 1, 5, 2, 4};
   auto it2 = {5, 2, 4};
   priority_queue<int> q1(less<int>(), it1);
   priority_queue<int> q2(less<int>(), it2);
   q1.swap(q2);
   cout << "Contents of q1 after swap operation" << endl;
   while (!q1.empty()) {
      cout << q1.top() << endl;
      q1.pop();
   }
   cout << "Contents of q2 after swap operation" << endl;
   while (!q2.empty()) {
      cout << q2.top() << endl;
      q2.pop();
   }
   return 0;
}

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

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