當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Queue priority_queue()用法及代碼示例


描述

C++ 移動構造函數std::priority_queue::priority_queue()使用移動語義用 other 的內容構造 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 的底層容器的類型,它的默認值為向量

返回值

構造函數從不返回值。

異常

此成員函數從不拋出異常。

時間複雜度

線性,即 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

相關用法


注:本文由純淨天空篩選整理自 C++ Queue Library - priority_queue() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。