C++ 佇列庫 - queue() 函式



描述

C++ 複製建構函式 std::priority_queue::priority_queue() 使用現有優先佇列中每個元素的副本構造一個優先佇列。other.

宣告

以下是來自 std::queue 標頭檔案的 std::priority_queue::priority_queue() 建構函式的宣告。

C++11

priority_queue( const priority_queue& other );

引數

other − 另一個相同型別的優先佇列物件。

返回值

建構函式不返回值。

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 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
queue.htm
廣告
© . All rights reserved.