C++ STL priority_queue, push(), pop(), top(), empty() and size() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: -

To do: Using the empty() to test if the priority_queue is empty, pop() to remove the largest element of the priority_queue from the top position, push() to add an element to the priority queue based on the priority of the element from operator<, size() to return the number of elements in the priority_queue and top() to return a const reference to the largest element at the top of the priority_queue

To show: How to use the C++ priority queue, push(), pop(), top(), empty() and size(). When you call pop() for the priority queue, you don't get the oldest element in the queue (as normal queue- FIFO). Instead, you get the element with the highest priority

 

// priority _queue, pop(), push(), top(), empty() and size()

#include <queue>

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// container of type integer

priority_queue <int> mypq1;

 

// push integers

mypq1.push(34);

mypq1.push(12);

mypq1.push(21);

mypq1.push(14);

 

// start from top and pop up

while ( !mypq1.empty())

{

cout<< mypq1.top()<<endl;

mypq1.pop();

}

 

// queue is empty

//

// push similar integers again

mypq1.push(34);

mypq1.push(12);

mypq1.push(21);

mypq1.push(14);

 

priority_queue <int>::size_type i, iii;

i = mypq1.size();

cout<<"The priority_queue length is "<<i<<endl;

const int& ii = mypq1.top();

cout<<"\nThe element at the top of the priority_queue is "<<ii<<endl;

mypq1.pop();

iii = mypq1.size();

cout<<"\nAfter a pop, the priority_queue length is "<<iii<<endl;

const int& iv = mypq1.top();

cout<<"\nAfter another pop, the element at the top of the "<<"priority_queue is "<<iv<<endl;

cout<<endl<<"Another example using string"<<endl;

// creates a priority queue mypq2 to store strings, and initializes the queue to be empty

priority_queue <string> mypq2;

mypq2.push("first push");

mypq2.push("second push");

mypq2.push("third push");

mypq2.push("fourth push");

// the strings are ordered inside the priority queue in lexicographic (dictionary) order:

// "third push", "second push", "fourth push", "first push"

// the lowest priority string is "first push", and the highest priority string is "third push"

while (!mypq2.empty())

{

cout<<mypq2.top()<<endl; // print the highest priority string

mypq2.pop(); // remove the highest priority string

}

return 0;

}

 

Output example:

 

34

21

14

12

The priority_queue length is 4

The element at the top of the priority_queue is 34

After a pop, the priority_queue length is 3

After another pop, the element at the top of the priority_queue is 21

Another example using string

third push

second push

fourth push

first push

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index