共用方式為


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
    型別 const 用來 Traits 的比較函式以 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;
}

Output

q1 = ( )
q2 = ( 15 10 5 )
After printing, q2 has 0 elements.
q3 = ( 1 2 3 )
q4 = ( 200 100 )
v5 = ( 10 30 20 )
q5 = ( 30 10 )
q6 = ( 10 30 )

需求

標題: <佇列>

命名空間: std

請參閱

參考

priority_queue 類別

標準樣板程式庫