priority_queue 類別

範本容器配接器類別,它提供的限制功能可限制存取某些基礎容器類型的最上層項目,且這一律為最大或最高優先順序。 新元素可以新增至 , priority_queue 而且可以檢查或移除 的 priority_queue 頂端元素。

語法

template <class Type, class Container= vector <Type>, class Compare= less <typename Container ::value_type>>
class priority_queue

參數

Type
要存放在 priority_queue 中的項目資料類型。

Container
用來實作 的基礎容器型別 priority_queue

Compare
型別,提供函式物件,可比較兩個元素值做為排序索引鍵,以判斷其在 中的 priority_queue 相對順序。 這個引數是選用引數,且預設值是二元述詞 less<typename Container::value_type>

備註

佇列物件第一個樣板參數中規定之類別 Type 的元素與 同義 value_type ,而且必須符合第二個樣板參數所規定之基礎容器類別 Container 中的元素類型。 Type必須可指派 ,才能複製該類型的物件,並將值指派給該類型的變數。

priority_queue 由呼叫 類別 Traits 的預存函式物件,排序它所控制的順序。 一般而言,元素只需要小於可比較才能建立這個順序:因此,只要有任兩個元素,就可以判斷它們相等(從意義上說兩者都小於另一個元素),或一個小於另一個元素。 這會導致非對等元件之間的排序。 一個技術提示,比較函式是在標準數學概念上產生嚴格弱式順序的二元述詞。

適用于 的適用基礎容器類別包括 dequepriority_queue Class 和預設 vector Class ,或任何其他支援 、 push_backpop_back 和 作業 front 的序列容器,以及隨機存取反覆運算器。 基礎容器類別會封裝在容器介面卡內,它只會公開有限的序列容器成員函式集做為公用的介面。

將項目加入至 priority_queue 或從中移除項目,都具有對數複雜度。 存取 priority_queue 中的項目具有常數複雜度。

C++ 標準程式庫定義了三種類型的容器配接器: stackqueuepriority_queue 。 每個類型都會限制某些基礎容器類別的功能,以精確地提供標準資料結構受控制的介面。

  • 類別 stack 支援最後一個先出 (LIFO) 資料結構。 就好像盤子的堆疊一樣,這是一種較為貼切好記的類比。 項目 (盤子) 可能會插入、檢查,或只從堆疊頂端移除,這是基底容器尾端的最後一個項目。 只存取 top 元素的限制是使用 stack 類別的原因。

  • 類別 queue 支援先進先出 (FIFO) 資料結構。 就好像人們排隊等候銀行櫃員一樣,這是一種較為貼切好記的類比。 項目 (人) 可能會加入隊伍的尾端,以及從隊伍的前面移除。 隊伍的前端和後端都可能會進行檢查。 以這種方式只存取前端和後端元素的限制,是使用 queue 類別的原因。

  • 類別 priority_queue 會排序其元素,讓最大元素一律位於頂端位置。 它支援插入項目,以及檢查和移除頂端項目。 記住的一個很好的類比是人們排隊,他們按年齡、身高或其他一些標準排列。

建構函式

建構函式 描述
priority_queue 建構 priority_queue,它可以是空的,或是基底容器物件範圍的複本,或是其他 priority_queue 的複本。

Typedefs

類型名稱 描述
container_type 提供基底容器以讓 priority_queue 調整的類型。
size_type 不帶正負號的整數類型,可以表示 priority_queue 中的項目數。
value_type 此類型代表儲存為 priority_queue 項目的物件類型。

成員函式

成員函數 描述
empty 測試 priority_queue 是否為空白。
pop 從頂端位置移除 priority_queue 的最大項目。
push 根據 專案的 operator< 優先順序,將專案加入至優先順序佇列。
size 傳回 priority_queue 中項目的數目。
top 傳回 priority_queue 頂端最大項目的 const 參考。

需求

標頭:<queue>

命名空間:std

priority_queue::container_type

提供要配接之基底容器的類型。

typedef Container container_type;

備註

此類型是範本參數 Container的同義字。 C++ 標準程式庫序列容器類別 deque 和預設類別 vector 符合做為物件基底容器 priority_queue 的需求。 也可以使用滿足該要求的使用者定義類型。

如需 的詳細資訊 Container ,請參閱類別 主題的 priority_queue 一節。

範例

如需如何宣告和使用 container_type 的範例 priority_queue ,請參閱範例。

priority_queue::empty

測試 priority_queue 是否為空白。

bool empty() const;

傳回值

true 如果 是空的 priority_queue ,則為 , false 如果 為空,則 priority_queue 為 。

範例

// pqueue_empty.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares priority_queues with default deque base container
   priority_queue <int> q1, s2;

   q1.push( 1 );

   if ( q1.empty( ) )
      cout << "The priority_queue q1 is empty." << endl;
   else
      cout << "The priority_queue q1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The priority_queue s2 is empty." << endl;
   else
      cout << "The priority_queue s2 is not empty." << endl;
}
The priority_queue q1 is not empty.
The priority_queue s2 is empty.

priority_queue::pop

從頂端位置移除 priority_queue 的最大項目。

void pop();

備註

priority_queue必須是空的 ,才能套用成員函式。 頂 priority_queue 端一律由容器中最大的元素所佔用。

範例

// pqueue_pop.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue <int> q1, s2;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   priority_queue <int>::size_type i, iii;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;

   q1.pop( );

   iii = q1.size( );
   cout << "After a pop, the priority_queue length is "
        << iii << "." << endl;

   const int& iv = q1.top( );
   cout << "After a pop, the element at the top of the "
        << "priority_queue is " << iv << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.
After a pop, the priority_queue length is 2.
After a pop, the element at the top of the priority_queue is 20.

priority_queue::priority_queue

priority_queue建構空的 ,或是基底容器物件或另一個 priority_queue 的複本。

priority_queue();

explicit priority_queue(const Traits& _comp);

priority_queue(const Traits& _comp, const container_type& _Cont);

priority_queue(const priority_queue& right);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Traits& _comp);

template <class InputIterator>
priority_queue(InputIterator first, InputIterator last, const Traits& _comp, const container_type& _Cont);

參數

_comp
型別的 constTraits 比較函式,用來排序 中的 priority_queue 專案,預設為比較基底容器的函式。

_Cont
建構 priority_queue 的基底容器是複本。

right
priority_queue建構集為複本的 。

first
要複製的元素範圍中第一個元素的位置。

last
超出要複製之元素範圍的第一個元素的位置。

備註

前三個建構函式中的每一個都會指定空的初始 priority_queue ,第二個建構函式也會指定要用於建立元素順序的比較函 comp 式類型,以及第三個明確指定 container_type 要使用的 ( _Cont )。 關鍵字 explicit 會隱藏某些類型的自動類型轉換。

第四個建構函式會指定 的 priority_queue right 複本。

最後三個建構函式會複製一些容器的範圍 [first, last) ,並使用 值來初始化 priority_queue ,並在指定 類別 Traitscontainer_type 的比較函式類型時越來越明確。

範例

// pqueue_ctor.cpp
// compile with: /EHsc
#include <queue>
#include <vector>
#include <deque>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   // The first member function declares priority_queue
   // with a default vector base container
   priority_queue <int> q1;
   cout << "q1 = ( ";
   while ( !q1.empty( ) )
   {
      cout << q1.top( ) << " ";
      q1.pop( );
   }
   cout << ")" << endl;

   // Explicitly declares a priority_queue with nondefault
   // deque base container
   priority_queue <int, deque <int> > q2;
   q2.push( 5 );
   q2.push( 15 );
   q2.push( 10 );
   cout << "q2 = ( ";
   while ( !q2.empty( ) )
   {
      cout << q2.top( ) << " ";
      q2.pop( );
   }
   cout << ")" << endl;

   // This method of printing out the elements of a priority_queue
   // removes the elements from the priority queue, leaving it empty
   cout << "After printing, q2 has " << q2.size( ) << " elements." << endl;

   // The third member function declares a priority_queue
   // with a vector base container and specifies that the comparison
   // function greater is to be used for ordering elements
   priority_queue <int, vector<int>, greater<int> > q3;
   q3.push( 2 );
   q3.push( 1 );
   q3.push( 3 );
   cout << "q3 = ( ";
   while ( !q3.empty( ) )
   {
      cout << q3.top( ) << " ";
      q3.pop( );
   }
   cout << ")" << endl;

   // The fourth member function declares a priority_queue and
   // initializes it with elements copied from another container:
   // first, inserting elements into q1, then copying q1 elements into q4
   q1.push( 100 );
   q1.push( 200 );
   priority_queue <int> q4( q1 );
   cout << "q4 = ( ";
   while ( !q4.empty( ) )
   {
      cout << q4.top( ) << " ";
      q4.pop( );
   }
   cout << ")" << endl;

   // Creates an auxiliary vector object v5 to be used to initialize q5
   vector <int> v5;
   vector <int>::iterator v5_Iter;
   v5.push_back( 10 );
   v5.push_back( 30 );
   v5.push_back( 20 );
   cout << "v5 = ( " ;
   for ( v5_Iter = v5.begin( ) ; v5_Iter != v5.end( ) ; v5_Iter++ )
      cout << *v5_Iter << " ";
   cout << ")" << endl;

   // The fifth member function declares and
   // initializes a priority_queue q5 by copying the
   // range v5[ first,  last) from vector v5
   priority_queue <int> q5( v5.begin( ), v5.begin( ) + 2 );
   cout << "q5 = ( ";
   while ( !q5.empty( ) )
   {
      cout << q5.top( ) << " ";
      q5.pop( );
   }
   cout << ")" << endl;

   // The sixth member function declares a priority_queue q6
   // with a comparison function greater and initializes q6
   // by copying the range v5[ first,  last) from vector v5
   priority_queue <int, vector<int>, greater<int> >
      q6( v5.begin( ), v5.begin( ) + 2 );
   cout << "q6 = ( ";
   while ( !q6.empty( ) )
   {
      cout << q6.top( ) << " ";
      q6.pop( );
   }
   cout << ")" << endl;
}

priority_queue::push

根據 專案的 operator< 優先順序,將專案加入至優先順序佇列。

void push(const Type& val);

參數

val
加入至 頂端的專案 priority_queue

備註

頂端 priority_queue 是容器中最大元素所佔用的位置。

範例

// pqueue_push.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue<int> q1;

   q1.push( 10 );
   q1.push( 30 );
   q1.push( 20 );

   priority_queue<int>::size_type i;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.

priority_queue::size

傳回 priority_queue 中項目的數目。

size_type size() const;

傳回值

的目前長度 priority_queue

範例

// pqueue_size.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue <int> q1, q2;
   priority_queue <int>::size_type i;

   q1.push( 1 );
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   q1.push( 2 );
   i = q1.size( );
   cout << "The priority_queue length is now " << i << "." << endl;
}
The priority_queue length is 1.
The priority_queue length is now 2.

priority_queue::size_type

不帶正負號的整數類型,可以表示 priority_queue 中的項目數。

typedef typename Container::size_type size_type;

備註

此類型與 所 priority_queue 調整之基底容器的 同義 size_type 字。

範例

如需如何宣告和使用 size_type 的範例 size ,請參閱範例。

priority_queue::top

傳回 priority_queue 頂端最大項目的 const 參考。

const_reference top() const;

傳回值

由 函式所 Traitspriority_queue 決定之最大專案的參考。

備註

priority_queue必須是空的 ,才能套用成員函式。

範例

// pqueue_top.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   priority_queue<int> q1;

   q1.push( 10 );
   q1.push( 30 );
   q1.push( 20 );

   priority_queue<int>::size_type i;
   i = q1.size( );
   cout << "The priority_queue length is " << i << "." << endl;

   const int& ii = q1.top( );
   cout << "The element at the top of the priority_queue is "
        << ii << "." << endl;
}
The priority_queue length is 3.
The element at the top of the priority_queue is 30.

priority_queue::value_type

此類型代表儲存為 priority_queue 項目的物件類型。

typedef typename Container::value_type value_type;

備註

此類型與 所 priority_queue 調整之基底容器的 同義 value_type 字。

範例

// pqueue_value_type.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares priority_queues with default deque base container
   priority_queue<int>::value_type AnInt;

   AnInt = 69;
   cout << "The value_type is AnInt = " << AnInt << endl;

   priority_queue<int> q1;
   q1.push( AnInt );
   cout << "The element at the top of the priority_queue is "
        << q1.top( ) << "." << endl;
}
The value_type is AnInt = 69
The element at the top of the priority_queue is 69.

另請參閱

C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考