
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <priority_queue>
The C++ Priority_queue is a container adaptor from the STL that provides the dynamic priority queue data structure. It is implemented as a binary heap, allowing efficient insertion, removal and access to the highest or lowest priority elements. By default, it orders elements in descending order based on a comparator function.Priority queues are commonly used in algorithms like Dijkstra's shortest path and Prim's minimum spanning tree.
Syntax
Below is the syntax of std::priority_queue.
template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> < class priority_queue;
Parameters
-
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Container − Type of the underlying container object.
-
Compare − Comparison object to be used to order the priority_queue.
This may be a function pointer or function object that can compare its two arguments.
Member types
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | value_type | T (First parameter of the template) |
2 | container_type | Second parameter of the template |
3 | size_type | size_t |
4 | reference | value_type& |
5 | const_reference | const value_type& |
6 | difference_type | ptrdiff_t |
Functions from <priority_queue>
Below is list of all methods from <priority_queue> .
Constructors
Sr.No. | Method & Description |
---|---|
1 |
priority_queue::priority_queue default constructor Constructs an empty priority_queue with zero element. |
2 |
priority_queue::priority_queue initialize constructor Constructs a priority_queue object and assigns internal container by a copy of ctnr. |
3 |
priority_queue::priority_queue range constructor Constructs a priority_queue with as many elements in range of first to last. |
4 |
priority_queue::priority_queue move constructor Constructs the priority_queue with the contents of other using move semantics. |
5 |
priority_queue::priority_queue copy constructor Constructs a priority_queue with copy of each elements present in existing priority_queue other. |
Destructor
Sr.No. | Method & Description |
---|---|
1 |
priority_queue::~priority_queue
Destroys priority_queue by deallocating container memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 |
priority_queue::emplace
Constructs and inserts new element in sorted order in the priority_queue. |
2 |
priority_queue::empty
Tests whether pritority_queue is empty or not. |
3 |
priority_queue::operator= Assigns new contents to the priority_queue by replacing old ones. |
4 |
priority_queue::pop
Removes front element of the priority_queue. |
5 |
priority_queue::push Inserts new element in sorted order. |
6 |
priority_queue::size
Returns the total number of elements present in the priority_queue. |
7 |
priority_queue::swap
Exchanges the contents of priority_queue with contents of another priority_queue. |
8 |
priority_queue::top
Returns a reference to the first element of the priority_queue |