Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class
Copyright © Curt Hill Introduction Like the STL stacks and queues this is an adaptor It may use a vector or deque –Vector is the default It uses the heap organization with the vector or deque
Generalities The include is the same as queue: #include The declaration is: priority_queue pq; –Requires that Acls is suitable for comparison You may also be explicit priority_queue, less > pq; This require include for vector Copyright © Curt Hill
Commentary The second template parameter is the actual container class You may substitute greater for less This makes the lowest value to be the next one obtained, rather than the greatest Copyright © Curt Hill
Methods The usual standard methods exist The queue specific ones that we are interested in are: –push –pop – void function –top – gives the top of the heap –empty – same as size() == 0 Copyright © Curt Hill
Example Copyright © Curt Hill #include using namespace std; int main () { priority_queue 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