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



描述

C++ 移动构造函数 std::priority_queue::priority_queue() 使用移动语义构造优先级队列,其内容来自另一个优先级队列。

声明

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

C++11

explicit priority_queue(const Compare& comp = Compare(),
                        Container&& ctnr = Container());

参数

  • compare − 用于对优先级队列进行排序的比较对象。

    这可能是一个函数指针或函数对象,可以比较其两个参数。

  • cntr − 容器对象。

    这是优先级队列底层容器的类型,其默认值为vector.

返回值

构造函数永远不会返回值。

异常

此成员函数永远不会抛出异常。

时间复杂度

线性,即 O(n)

示例

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

#include <iostream>
#include <queue>

using namespace std;

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

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

   cout << endl;

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

   return 0;
}

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

Contents of q1 after move operation

Contents of q2 after move operation
5
4
3
2
1
queue.htm
广告

© . All rights reserved.