STL priority_queue使用
2012-10-02 13:56 youxin 阅读(359) 评论(0) 编辑 收藏 举报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 thepriority 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:
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 orpop, respectively) to grant that the element popped is always the greatest 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.
top默认是返回顶端的元素(比较高的元素)。
Returns a constant reference to the top element in the 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::popis called.
注意:stack方法顶端元素是s.top()方法,没有front()和back()方法,而deque是d.front()方法。
而priority_queue则是top()方法。
// 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; }
Popping out elements... 100 40 30 25
构造函数几种不同的方式:
// 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.
如果执行:
for(int i=0;i<4;i++) fifth.push(*(myints+i)); for(int j=0;j<4;j++) { cout<<fifth.top()<<endl; fifth.pop(); }
输出 60 50 20 10
若是sixth,输出相反。