Вы находитесь на странице: 1из 15

priority_queue

class template
<queue>

Priority queue Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. This context is similar to a heap where only the max heap element can be retrieved (the one at the top in the priority queue) and elements can be inserted indefinitely. Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue. The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it must be accessible through random access iterators and it must support the following operations:
front() push_back() pop_back()

Therefore, the standard container class templates vector and deque can be used. By default, if no container class is specified for a particular priority_queue class, the standard container class template vector is used. Support for random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by calling the algorithms make_heap, push_heap and pop_heap when appropriate. In their implementation in the C++ Standard Template Library, priority queues take three template parameters:
1 2 template < class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;

Where the template parameters have the following meanings:


T: Type of

the elements.

Container: Type of

the underlying container object used to store and access the

elements. Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function. This defaults to less<T>, which returns the same as applying the less-than operator (a<b). The priority_queue object uses this expression when an element is inserted or removed from it (using push or pop, respectively) to grant that the element popped is always the greater in the priority queue.

In the reference for the priority_queue member functions, these same names (T, Container and Compare) are assumed for the template parameters.

Member functions
(constructor) Construct priority queue (public member function ) empty Test whether container is empty (public member function) size Return size (public member function) top Access top element (public member function) push Insert element (public member function) pop Remove top element (public member function)

C++ : Reference : STL Containers : priority_queue : priority_queue Search: login: sign in using: remember me [register]

cplusplus.com Information Documentation

priority_queue::priority_queu e

Reference Articles Forum Reference C Library IOstream Library Strings library STL Containers STL Algorithms Miscellaneous STL Containers bitset deque list map multimap multiset priority_queue queue set stack vector

public member function


explicit priority_queue ( const Compare& x = Compare(), const Container& y = Container() ); template <class InputIterator> priority_queue ( InputIterator first, InputIterator last, const Compare& x = Compare(), const Container& y = Container() );

Construct priority queue


Constructs a priority_queue container adaptor object. A container adaptor keeps a container object as data. This container object is a copy of parameter y, if any, otherwise it is an empty container. Because priority_queues satisfy the heap property the element popped from it is always the highest in the container. But the exact ordering criterium to determine this may be any that follow a strict weak ordering, which may be modified with an appropiate y parameter. The function effectively initializes the comparison and container objects and calls algorithm make_heap accordingly.

Parameters
x Comparison object to be used for the strict weak ordering. Compare is the third class template parameter (see class description). y Container object. Container is the second class template parameter (the type of the underlying container for the priority_queue; by default: vector<T>, see class description). first,last

priority_queue priority_queue::priority_queu e member functions: priority_queue::empty

Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element pointed by first but not the element pointed by last. The function template type can be any type of input iterator.

Example
priority_queue::pop priority_queue::push priority_queue::size priority_queue::top
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // constructing priority queues #include <iostream> #include <queue> using namespace std; class mycomparison { bool reverse; public: mycomparison(const bool& revparam=false) {reverse=revparam;} bool operator() (const int& lhs, const int&rhs) const { if (reverse) return (lhs>rhs); else return (lhs<rhs); } }; int main () { int myints[]= {10,60,50,20}; priority_queue<int> first; priority_queue<int> second (myints,myints+4); priority_queue< int, vector<int>, greater<int> > third (myints,myints+4); // using mycomparison: priority_queue< int, vector<int>, mycomparison > fourth; typedef priority_queue<int,vector<int>,mycomparison> mypq_type; mypq_type fifth (mycomparison()); mypq_type sixth (mycomparison(true)); return 0; }

The example does not produce any output, but it constructs different priority_queue objects: First is empty. Second contains the four ints defined for myints, with 60 (the highest) at its top. Third has the same four ints, but because it uses greater instead of the default (which is less), it has 10 as its top element. Fourth, fifth and sixth are very similar to first: they are all empty, except that these use mycomparison for comparisons, which is a special comparison function that behaves differently depending on a flag set on construction.

Complexity
Up to linear in the number of initial elements. Home page | Privacy policy cplusplus.com, 2000-2011 - All rights reserved - v2.9b Spotted an error? contact us

C++ : Reference : STL Containers : priority_queue : empty Search: login: sign in using: cplusplus.com Information Documentation Reference Articles remember me [register]

priority_queue::empty
public member function
bool empty ( ) const;

Test whether container is empty

Forum Reference C Library IOstream Library Strings library STL Containers STL Algorithms Miscellaneous STL Containers bitset deque list map multimap multiset priority_queue queue set stack vector priority_queue

Returns whether the priority_queue is empty, i.e. whether its size is 0. This member function effectively calls the member with the same name in the underlying container object.

Parameters
none

Return Value
true if the container size is 0, false otherwise.

Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// priority_queue::empty #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> mypq; int sum (0); for (int i=1;i<=10;i++) mypq.push(i); while (!mypq.empty()) { sum += mypq.top(); mypq.pop(); } cout << "total: " << sum << endl; return 0; }

The example initializes the content of the priority queue to a sequence of numbers (form 1 to 10). It then pops the elements one by one until it is empty and calculates their sum.

priority_queue::priority_queue Output:
total: 55

member functions: priority_queue::empty priority_queue::pop priority_queue::push priority_queue::size priority_queue::top

Complexity
Constant.

See also
priority_queue::size Return size (public member function)

Home page | Privacy policy cplusplus.com, 2000-2011 - All rights reserved - v2.9b Spotted an error? contact us

C++ : Reference : STL Containers : priority_queue : size Search: login: sign in using: remember me [register]

cplusplus.com Information Documentation Reference Articles

priority_queue::size
public member function
size_type size ( ) const;

Return size
Returns the number of elements in the priority_queue.

Forum Reference C Library IOstream Library Strings library STL Containers STL Algorithms Miscellaneous STL Containers bitset deque list map multimap multiset priority_queue queue set stack vector priority_queue priority_queue::priority_queue Output:
0. size: 0 1. size: 5 2. size: 4

This member function effectively calls the member with the same name in the underlying container object.

Parameters
none

Return Value
The number of elements that conform the priority_queue's container content. Member type size_type is an unsigned integral type.

Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // priority_queue::size #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> myints; cout << "0. size: " << (int) myints.size() << endl; for (int i=0; i<5; i++) myints.push(i); cout << "1. size: " << (int) myints.size() << endl; myints.pop(); cout << "2. size: " << (int) myints.size() << endl; return 0; }

Complexity

member functions: priority_queue::empty priority_queue::pop priority_queue::push priority_queue::size priority_queue::top

Constant.

See also
priority_queue::empty Test whether container is empty (public member function)

Home page | Privacy policy cplusplus.com, 2000-2011 - All rights reserved - v2.9b Spotted an error? contact us

C++ : Reference : STL Containers : priority_queue : top Search: login: sign in using: remember me [register]

cplusplus.com Information Documentation Reference Articles

priority_queue::top
public member function
const value_type& top ( ) const;

Access top element


Returns a constant reference to the top element in the priority_queue.

Forum Reference C Library IOstream Library Strings library STL Containers STL Algorithms Miscellaneous STL Containers bitset deque list map multimap multiset priority_queue queue set stack vector priority_queue

The top element is the element that compares higher in the priority_queue, and the next that is removed from the container when priority_queue::pop is called. This member function effectively calls the member function front of the underlying container object.

Parameters
none

Return value
A reference to the top element in the priority_queue. Member type value_type is defined to the type of value contained by the underlying container, which shall be the same as the first template parameter (T).

Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // priority_queue::top #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> mypq; mypq.push(10); mypq.push(20); mypq.push(15); cout << "mypq.top() is now " << mypq.top() << endl; return 0; }

Output:

priority_queue::priority_queue mypq.top() is now 20

member functions: priority_queue::empty priority_queue::pop priority_queue::push priority_queue::size priority_queue::top

Complexity
Constant.

See also
priority_queue::pop Remove top element (public member function) priority_queue::push Insert element (public member function)

Home page | Privacy policy cplusplus.com, 2000-2011 - All rights reserved - v2.9b Spotted an error? contact us

C++ : Reference : STL Containers : priority_queue : push Search: login: sign in using: remember me [register]

cplusplus.com Information Documentation Reference Articles

priority_queue::push
public member function
void push ( const T& x );

Insert element
Inserts a new element in the priority_queue. The content of this new

Forum Reference C Library IOstream Library Strings library STL Containers STL Algorithms Miscellaneous STL Containers bitset deque list

element is initialized to a copy of x. This member function effectively calls the member function push_back of the underlying container object, and then calls the push_heap algorithm to keep the heap property of priority_queues.

Parameters
x Value to be copied to the new element. T is the first template parameter (the type of the elements stored in the stack).

Return value
none

Example
// priority_queue::push/pop #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << " " << mypq.top(); mypq.pop(); } cout << endl; return 0; }

1 2 map 3 4 5 multimap 6 7 multiset 8 9 priority_queue 10 11 queue 12 13 set 14 15 16 stack 17 18 vector 19 20 priority_queue 21 22 priority_queue::priority_queue 23

member functions: priority_queue::empty priority_queue::pop priority_queue::push priority_queue::size

24

Output:
Popping out elements... 100 40 30 25

Complexity
priority_queue::top Constant (in the priority_queue). Although notice that push_heap operates in logarithmic time.

See also
priority_queue::pop Remove top element (public member function) priority_queue::size Return size (public member function) Home page | Privacy policy cplusplus.com, 2000-2011 - All rights reserved - v2.9b Spotted an error? contact us

priority_queue::pop
public member function
void pop ( );

Remove top element


Removes the element on top of the priority_queue, effectively reducing its size by one. The value of this element can be retrieved before being popped by calling member priority_queue::top. This calls the removed element's destructor. This member function effectively calls the pop_heap algorithm to keep the heap property of priority_queues and then calls the member function pop_back of the underlying container object to remove the element.

Parameters

none

Return value
none

Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// priority_queue::push/pop #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << " " << mypq.top(); mypq.pop(); } cout << endl; return 0; }

Output:
Popping out elements... 100 40 30 25

Complexity
Constant (in the priority_queue). Although notice that pop_heap operates on logarithmic time.

See also

priority_queue::push Insert element (public member function) priority_queue::empty Test whether container is empty (public member function)

Вам также может понравиться