The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <queue>
Introduction to queue
Queue is a data structure designed to operate in FIFO (First in First out) context. In queue elements are inserted from rear end and get removed from front end.
Queue class is container adapter. Container is an objects that hold data of same type. Queue can be created from different sequence containers. Container adapters do not support iterators therefore we cannot use them for data manipulation. However they support push() and pop() member functions for data insertion and deletion respectively.
Definition
Below is definition of std::queuer from <queue> header file
template <class T, class Container = deque<T> > class 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.
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 <queue>
Below is pst of all methods from <queue> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty queue object, with zero elements. |
default constructor
2 | Constructs a queue object and assigns internal container by a copy of ctnr. |
initiapze constructor
3 | Constructs the queue with the contents of other using move semantics. |
move constructor
4 | Constructs a queue with copy of each elements present in existing queue other. |
copy constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys queue by deallocating container memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Returns a reference to the last element of queue. |
2 | Constructs and inserts new element at the end of queue. |
3 | Tests whether queue is empty or not. |
4 | Returns a reference to the first element of the queue. |
5 | Assigns new contents to the queue by replacing old ones. |
copy version
6 | Assigns new contents to the queue by replacing old ones. |
move version
7 | Removes front element of the queue. |
8 | Inserts new element at the end of queue. |
copy version
9 | Inserts new element at the end of queue. |
move version
10 | Returns the total number of elements present in the queue. |
11 | Exchanges the contents of queue with contents of another queue. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two queues are equal or not. |
2 | Tests whether two queues are equal or not. |
3 | Tests whether first queue is less than other or not. |
4 | Tests whether first queue is less than or equal to other or not. |
5 | Tests whether first queue is greater than other or not. |
6 | Tests whether first queue is greater than or equal to other or not. |
7 | Exchanges the contents of two queues. |
Introduction to priority_queue
Priority queue is queue data structure that holds priority. Priority queue is analogous to heap data structure where element can be inserted in any order and always max heap element is retrieved first.
Definition
Below is definition of std::priority_queue from <queue> header file
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 <queue>
Below is pst of all methods from <queue> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty priority_queue with zero element. |
default constructor
2 | Constructs a priority_queue object and assigns internal container by a copy of ctnr. |
initiapze constructor
3 | Constructs a priority_queue with as many elements in range of first to last. |
range constructor
4 | Constructs the priority_queue with the contents of other using move semantics. |
move constructor
5 | Constructs a priority_queue with copy of each elements present in existing priority_queue other. |
copy constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys priority_queue by deallocating container memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Constructs and inserts new element in sorted order in the priority_queue. |
2 | Tests whether pritority_queue is empty or not. |
3 | Assigns new contents to the priority_queue by replacing old ones. |
copy version
4 | Assigns new contents to the priority_queue by replacing old ones. |
move version
5 | Removes front element of the priority_queue. |
6 | Inserts new element in sorted order. |
copy version
7 | Inserts new element in sorted order. |
move version
8 | Returns the total number of elements present in the priority_queue. |
9 | Exchanges the contents of priority_queue with contents of another priority_queue. |
10 | Returns a reference to the first element of the priority_queue |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Exchanges the contents of priority_queue with contents of another priority_queue. |