priority_queue |
C++ Library |
explicit priority_queue(const Pred& pr = Pred(), const allocator_type& a1 = allocator_type())
The default constructor creates an empty priority_queue.
priority_queue(const value_type* first, const value_type* last,
const Pred& pr = Pred(), const allocator_type& al = allocator_type())
This initializes the priority_queue with copies of elements in the range [first, last). The function object pr is used to order the elements in the priority_queue.
If you specify a container type when creating a priority_queue, the priority_queue will be stored in the specified container type. The default container used to hold the priority_queue is a vector<T>.
If you specify a function object, priority_queue will use the specified function object for sorting items. The default function object used to sort items is less<Cont::value_type>.
//Compiler options: /GX
#include <queue>
#include <functional>
#include <iostream>
int main()
{
std::priority_queue<int> pqi ; //Constructs an empty priority_queue, uses vector as default container.
int i ;
std::priority_queue<int>::allocator_type a1 = pqi.get_allocator() ;
std::cout << "call pqi.empty()" << std::endl ;
if (pqi.empty())
{
std::cout << "priority_queue is empty" << std::endl ;
}
else
{
std::cout << "priority_queue contains some elements" << std::endl ;
}
std::cout << "pqi.size() = " << pqi.size() << std::endl ;
std::cout << "Push Values on pqi = " ;
for(i = 0; i < 10; i++)
{
std::cout << i << ", " ;
pqi.push(i) ;
}
std::cout << std::endl ;
std::cout << "pqi.size() = " << pqi.size() << std::endl ;
std::cout << "Pop Values from pqi = " ;
while (!pqi.empty())
{
std::cout << pqi.top() << ", " ;
pqi.pop() ;
}
std::cout << std::endl ;
return 0 ;
}
call pqi.empty() priority_queue is empty pqi.size() = 0 Push Values on pqi = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, pqi.size() = 10 Pop Values from pqi = 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,