目录

priority_queue::priority_queue

描述 (Description)

C ++移动构造函数std::priority_queue::priority_queue()使用其他使用移动语义的内容构造priority_queue。

声明 (Declaration)

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

C++11

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

参数 (Parameters)

  • compare - 用于对priority_queue进行排序的比较对象。

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

  • cntr - 容器对象。

    这是priority_queue的基础容器的类型,它的默认值是vector

返回值

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

异常 (Exceptions)

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

时间复杂

线性即O(n)

例子 (Example)

以下示例显示了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
↑回到顶部↑
WIKI教程 @2018