C++ STl : priority_queues

Priority_Queue

  • A container that provides constant-time lookup of the largest element(by default)
  • But, the insertion and exertion are logarithmic in nature
  • A user-defined Compare function can also be passed in as a parameter.
  • greater<int> would cause the smallest element to appear as the top()
Here is how you declare priority_queue:
                                                     priority_queue<int> q;
Here is one good trick: You can also create a priority_queue which keeps data in ascending order using the following syntax. Everything else remains the same:
                                        priority_queue<int, vector<int> ,std::greater<int> > q;

Member function

  • top() :: Access the top element
  • empty: Checks whether the underlying container is empty
  • size: returns the number of elements
  • push: inserts element and sorts the underlying container
  • emplace: constructs elements in-place and sorts the underlying container
  • pop: removes the top element
  • swap: swaps the contents
Here is the link to the official documentation of priority_queue: https://en.cppreference.com/w/cpp/container/priority_queue

No comments:

Post a Comment

Installing albert on ubuntu 19.04

Installing Albert on Ubuntu 19.04... Albert is not still released for ubuntu 19.04. But still, you can install it using the following ...