Home >>C++ Standard Template Library Tutorial(STL) >C++ Queue

Queue in C++

Queue in C++

Queue in C++ is basically the type of data type that is used in a large number of programs by the programmers. Queue generally works on the FIFO technique, when expanded, FIFO becomes First In First Out. That simply means that the element that was first inserted will be extracted at the first and the circle will keep revolving. Front is the element placed at the front most position or we can say on the first position and along with front there is an element known as 'rear' that is the element placed at the last position. The insertion of the elements takes place at the rear end and the deletion is performed from the front. Queues are generally implied as the container adaptors in the application areas.

Syntax

template<class T, class Container = deque<T> > class queue;  

Template Parameters

T : This argument generally specifies the type of the element that the container adaptor will be holding.

Container : This argument generally specifies the container’s internal object that is where the elements of the queues are held.

Here is the list of the containers whose support is required for these depicted operations:

  • empty()
  • size()
  • back()
  • push_back()
  • pop_front()
  • front()
  • back

C++ Queue Function

In order to play an object or a variable in the field of programming these functions are used. Queue generally delivers a variety of functions that are used or embedded in the programs. Here is a list of all the functions along with a short description:

Function Description
(constructor) This is the function that is generally used for the construction of a queue container
empty() This is the function that is generally used to test for the emptiness of a queue. If the queue is found to be empty then the function returns true else false.
size() This is the function that is generally used to return the size of the queue container, that is a measure of the number of elements stored in the queue.
front() This is the function that is generally used to access the front element of the queue. The element plays a key role as all the deletion operations are performed at the front element.
back() This is the function that is generally used to access the rear element of the queue. The element plays an imminent role as all the insertion operations are performed at the rear element.
push() This is the function that is generally used for the insertion of a new element at the rear end of the queue.
pop() This is the function that is generally used for the deletion of element; the element in the queue is deleted from the front end.
emplace() This is the function that is generally used for insertion of new elements in the queue above the current rear element.
swap() This is the function that is generally used for interchanging the contents of two containers in reference.
relational operators This non-number function is generally used to specify the relational operators that are needed for the queues.
uses allocator<queue> This non-number function is generally used to use the allocator for the queues.

Simple example of C++ Queue

#include <iostream>  
#include <queue>  
using namespace std;  
void showQueue(queue <int> que)  
{  
    queue <int> sq = que;  
    while (!sq.empty())  
    {  
        cout << '\t' << sq.front();  
        sq.pop();  
    }  
    cout << '\n';  
}  
  
int main()  
{  
    queue <int> fquiz;  
    fquiz.push(11);  
    fquiz.push(22);  
    fquiz.push(33);  
  
    cout << "Here is the  Queue  : ";  
    showQueue(fquiz);  
  
    cout << "\nSize of Queue : " << fquiz.size();  
    cout << "\nFront of Queue : " << fquiz.front();  
    cout << "\nBack of Queue : " << fquiz.back();  
	return 0;  
}  
Output :
Here is the Queue : 11 22 33
Size of Queue : 3
Front of Queue : 11
Back of Queue : 33

No Sidebar ads