C++ STL Container Fundamentals Priority Queue

The set of C++ template classes is known as the standard template class library (STL). It is a very powerful software library of a set of C++ template classes. It provides functions, iterators, containers, and built-in algorithms. Let us know about the ‘C++ STL Container Fundamentals Priority Queue’.

C++ STL Container Fundamentals Priority Queue

C++ STL container fundamentals priority queue

C++ priority container is the STL container adapter specially designed for regular queues, but the elements of queues are also associated with them. The first element of the queue can be the greatest either or smallest of all elements available in queues and elements can be in decreasing or no non-decreasing order. Every element present in the queue has a fixed priority. 

STL, C++, The topmost elements should always be the greatest in order. We can also optimize it by the smallest in order. An array or vector can be used as an internal structure in priority_queue. In simple words, STL Priority Queue can be described as the implementation of HEAP Data Structure.

 STL containers can be defined as an object that can store multiple elements, manage storage required for the elements, and allow us to access the member functions. The STL containers are divided majorly into three types:

A priority queue is described as a container adaptor that is constantly looking up for the largest element, at the expense of logarithmic extraction and insertion.

In the context of a random-access container, the priority queue implementation would store the elements in the container and maintain a heap structure over them. This has the benefit of not invalidating the heap when the container is modified, since the container’s indices can be used to maintain the heap property.

The template parameter T

The elements to be stored. If the T is not recognized as the same type as a container and its behaviour can be undefined sometimes.

1. Container: The Elements can be stored in a container. The container requires the sequencecontainer and the iterators require the LegacyRandomAccessIterator. It provides the following functions with these below semantics.

  • Push back()
  • Pop back()
  • Front()

2. Compare: Compare provides a rule for strict weak ordering.

3. Unordered associative container: unordered map, unordered multimap, unordered set ,and unordered multiset.

4. Associative container: map, multimap, set, and multiset.

5. Sequence container: arrays, list, vectors, forward list, and dequeue.

Use of the C++ STL Container priority_queue

Priority queues are used mainly for problem-solving

  • Maps: the highest priority should be given to the shortest path.
  • Interrupt handling
  • Traffic lights
  • Emergency queue
  • OS load balancing 
  • Data compression

Methods of priority_queue

There are some methods associated with priority queues in STL

Here priority queue::empty()

  • priority_queue::size() 
  • priority_queue::top()
  • priority_queue::push() 
  • priority_queue::pop()
  • priority_queue::swap()
  • priority_queue::emplace()

C++ STL Container priority_queue

Use of priority queue as C++ STL container for a smoother coding experience.

C++ STL Priority Queue Example Code

// C++ Queue in STL

#include <iostream>

 #include <queue>

 using namespace std; 

int main() { priority_queue<int> pq; pq.push(30);

 pq.push(100); 

pq.push(25); 

pq.push(40); 

cout << “Top element: ” << pq.top() << endl; cout << “Size: ” << pq.size() << endl;

 while (!pq.empty())

 { cout << pq.top() << ” “; pq.pop(); } cout << endl; return 0; } 

void printPriorityQueueMinHeap(priority_queue < int, vector < int > , greater < int > > PQ) {

  priority_queue < int, vector < int > , greater < int > > priorityQueueToPrint = PQ;

  while (!priorityQueueToPrint.empty()) {

   cout << ” ” << priorityQueueToPrint.top();

   priorityQueueToPrint.pop();

  }

  cout << “\n”;

}

int main() {

  priority_queue < int > intPriorityQueueExample;

  intPriorityQueueExample.push(2);

  intPriorityQueueExample.push(4);

  intPriorityQueueExample.push(6);

  intPriorityQueueExample.push(8);

  cout << “The priority queue intPriorityQueueExample contains (max heap):”;

  printPriorityQueue(intPriorityQueueExample);

  cout << “The size of intPriorityQueueExample is: ” << intPriorityQueueExample.size() << “\n”;

  cout << “The top-most element of intPriorityQueueExample is: ” << intPriorityQueueExample.top() << “\n”;

  cout << “intPriorityQueueExample after popping an element:”;

  intPriorityQueueExample.pop();

  printPriorityQueue(intPriorityQueueExample);

  cout << “Is intPriorityQueueExample empty?: ” << intPriorityQueueExample.empty() << “\n”;

  priority_queue < int > intPriorityQueueExample2;

  // Swapping intPriorityQueueExample with the empty intPriorityQueueExample2

  intPriorityQueueExample.swap(intPriorityQueueExample2);

  cout << “Is intPriorityQueueExample empty after swapping with intPriorityQueueExample2?: ” << intPriorityQueueExample.empty() << “\n”;

  // Note that we can also have a min heap priority queue as shown below

  priority_queue < int, vector < int > , greater < int > > intPriorityQueueExample3;

  intPriorityQueueExample3.push(10);

  intPriorityQueueExample3.push(40);

  intPriorityQueueExample3.push(20);

  intPriorityQueueExample3.push(30);

  cout << “The priority queue intPriorityQueueExample3 contains (min heap):”;

  printPriorityQueueMinHeap(intPriorityQueueExample3);

  return 0;

}

Output

The example of PriorityQueueExamoles contains (max heap): 8 6 4 2

The size example heap is: 4

The top-most element of Priority heap is: 8

   p1.push(49);

   p1.push(32);

   while (!p1.empty())

   {

     cout <<” “<< p1.top() << ” “;

     p1.pop();

   }

   return 0;  

}

C++ methods of priority queues

Following are the methods used in priority_queue. 

Methods Description 
empty()This method is used to check whether the priority_queue is empty or not.
size()
  This method provides the appropriate size and number of elements present in priority_queue. 
  push()This method is used to give input into the queue. First, the element is added to the last line of the queue, and then simultaneously the elements should be ordered correctly. 
top()This method shows the information about the top element from the queue container. It does not take any input. 
swap()
  This method is used to swap the elements in the queue with one another priority_queue of the same type and size. 
emplace()  This method in the container adds a new element at the top. It generally takes values in parameters

Conclusion

STL is a very powerful Software Library of C++ template class. The set of C++ template classes is generally known as Standard Template Class Library (STL).

The priority queue can be described as a container that contains elements with priority. Besides queues, which delete or insert the element based on the FIFO rule, in the priority queue, the elements can be removed based on their priority. The first element which is removed firstly be the highest priority element from the queue.

FAQS

1. What is priority_queue in C++?

C++ STL priority_queue is described as a container of elements like a regular STL queue, with the elements of queues having some priorities attached with it.

2. What are the functions in C++ STL priority_queue?

Push(), pop(), size(),swap(), empty(), top() ,and emplace() are the functions rthat are associated with the priority_queue in C++ STL.

C++ STL Container Fundamentals Priority Queue

Leave a Reply

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

Scroll to top