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.