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随着在指定选件类 Traits 和 container_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 )
要求
标头: <queue>
命名空间: std