C++ Library - <queue>


Advertisements


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.

S.N. 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 list of all methods from <queue> header.

Constructors

S.N. Method & Description
1 queue::queue default constructor

Constructs an empty queue object, with zero elements.

2 queue::queue initialize constructor

Constructs a queue object and assigns internal container by a copy of ctnr.

3 queue::queue move constructor

Constructs the queue with the contents of other using move semantics.

4 queue::queue copy constructor

Constructs a queue with copy of each elements present in existing queue other.

Destructor

S.N. Method & Description
1 queue::~queue

Destroys queue by deallocating container memory.

Member functions

S.N. Method & Description
1 queue::back

Returns a reference to the last element of queue.

2 queue::emplace

Constructs and inserts new element at the end of queue.

3 queue::empty

Tests whether queue is empty or not.

4 queue::front

Returns a reference to the first element of the queue.

5 queue::operator= copy version

Assigns new contents to the queue by replacing old ones.

6 queue::operator= move version

Assigns new contents to the queue by replacing old ones.

7 queue::pop

Removes front element of the queue.

8 queue::push copy version

Inserts new element at the end of queue.

9 queue::push move version

Inserts new element at the end of queue.

10 queue::size

Returns the total number of elements present in the queue.

11 queue::swap

Exchanges the contents of queue with contents of another queue.

Non-member overloaded functions

S.N. Method & Description
1 operator==

Tests whether two queues are equal or not.

2 operator!=

Tests whether two queues are equal or not.

3 operator<

Tests whether first queue is less than other or not.

4 operator<=

Tests whether first queue is less than or equal to other or not.

5 operator>

Tests whether first queue is greater than other or not.

6 operator>=

Tests whether first queue is greater than or equal to other or not.

7 swap

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.

S.N. 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 list of all methods from <queue> header.

Constructors

S.N. 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

S.N. Method & Description
1 priority_queue::~priority_queue

Destroys priority_queue by deallocating container memory.

Member functions

S.N. 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= copy version

Assigns new contents to the priority_queue by replacing old ones.

4 priority_queue::operator= move version

Assigns new contents to the priority_queue by replacing old ones.

5 priority_queue::pop

Removes front element of the priority_queue.

6 priority_queue::push copy version

Inserts new element in sorted order.

7 priority_queue::push move version

Inserts new element in sorted order.

8 priority_queue::size

Returns the total number of elements present in the priority_queue.

9 priority_queue::swap

Exchanges the contents of priority_queue with contents of another priority_queue.

10 priority_queue::top

Returns a reference to the first element of the priority_queue

Non-member overloaded functions

S.N. Method & Description
1 swap

Exchanges the contents of priority_queue with contents of another priority_queue.



Advertisements
E-Books Store