C++ 队列库 - queue() 函数



描述

C++ 复制构造函数 std::priority_queue::priority_queue() 使用现有优先队列中每个元素的副本构造一个优先队列。other.

声明

以下是来自 std::queue 头文件的 std::priority_queue::priority_queue() 构造函数的声明。

C++11

priority_queue( const priority_queue& other );

参数

other − 另一个相同类型的优先队列对象。

返回值

构造函数不返回值。

异常

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

时间复杂度

线性,即 O(n)

示例

以下示例演示了 std::priority_queue::priority_queue() 构造函数的用法。

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
   vector<int> v = {3, 1, 5, 2, 4};
   priority_queue<int> q1(less<int>(), v);
   priority_queue<int> q2(q1);

   cout << "Contents of q1" << endl;
   while (!q1.empty()) {
      cout << q1.top() << endl;
      q1.pop();
   }

   cout << endl;

   cout << "Contents of q2" << endl;
   while (!q2.empty()) {
      cout << q2.top() << endl;
      q2.pop();
   }

   return 0;
}

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

Contents of q1
5
4
3
2
1
Contents of q2
5
4
3
2
1
queue.htm
广告
© . All rights reserved.