CPP STL Tutorial : C++ std::priority_queue and it’s operations.

In this chapter we shall learn about:
1. priority_queue introduction.
2. priority_queue operations.
3. priority_queue member declaration.
4. priority_queue member functions

1. priority_queue introduction.

1. priority_queue is a type of container adapter.
2. It supports FIFO [First In First Out].
3. FIFO means, that the elements are inserted first will be removed first.
Below is the header file to be used for stack:
#include <queue>          // std::stack

2. priority_queue operations.

priority_queue will support below operations:
empty
size
front
back
push_back
pop_front

3. priority_queue member declaration.

std::priority_queue<int> mypriority_queue;
mypriority_queue.push (1);
mypriority_queue.push (2);
mypriority_queue.push (3);
mypriority_queue.push (4);

4. priority_queue member functions

empty : It will test whether container is empty
size : It will return size
push : It will insert element
pop : It will remove element
top   : It will access top element
#include <iostream>
#include <queue>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{

  std::priority_queue<int> mypq;
  mypq.push (1);
  mypq.push (2);
  mypq.push (3);
  mypq.push (4);
  
  cout<<"Size of mypq is "<< mypq.size()<<endl;
  cout<<"mypq elements are: "<<endl;

   while (!mypq.empty())
  {
     std::cout << ' ' << mypq.top();
     mypq.pop();
  }
  return 0;
}
Output:
Size of mypq is 4
mypq elements are:
4 3 2 1
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *