C++ Queue 库 - priority_queue() 函数
描述
C++ 移动构造函数std::priority_queue::priority_queue() 使用移动语义构造带有其他内容的 priority_queue。
声明
以下是 std::priority_queue::priority_queue() 构造函数形式 std::queue 头的声明。
C++11
explicit priority_queue(const Compare& comp = Compare(),
Container&& ctnr = Container());
参数
compare − 用于排序 priority_queue 的比较对象。
这可能是一个函数指针或函数对象,可以比较它的两个参数。
cntr − 容器对象。
这是priority_queue 的底层容器的类型,它的默认值为vector
。
返回值
构造函数从不返回值。
异常
此成员函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
以下示例显示了 std::prioiry_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

