C++ Queue 库 - queue() 函数
描述
C++ 复制构造函数 std::priority_queue::priority_queue() 构造一个 priority_queue,其中包含现有 priority_queue other 中存在的每个元素的副本。
声明
以下是 std::priority_queue::priority_queue() 构造函数形式 std::queue 头的声明。
C++11
priority_queue( const priority_queue& other );
参数
other − 另一个相同类型的 priority_queue 对象。
返回值
构造函数从不返回值。
异常
此成员函数从不抛出异常。
时间复杂度
线性,即 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