queue::front() and queue::back() in C++ STL

23 Jun, 2022
Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. 
This function is used to reference the first or the oldest element of the queue container. This function can be used to fetch the first element of a queue.Syntax :  
queuename.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the first element of the queue container.
Examples:  
Input  :  myqueue = 1, 2, 3
          myqueue.front();
Output :  1

Input  :  myqueue = 3, 4, 1, 7, 3
          myqueue.front();
Output :  3
Errors and Exceptions  
If the queue container is empty, it causes undefined behaviorIt has a no exception throw guarantee if the queue is not empty
If the queue container is empty, it causes undefined behavior
It has a no exception throw guarantee if the queue is not empty
 
CPP
// CPP program to illustrate// Implementation of front() function#include <iostream>#include <queue>using namespace std; int main(){    queue<int> myqueue;    myqueue.push(3);    myqueue.push(4);    myqueue.push(1);    myqueue.push(7);       // Queue becomes 3, 4, 1, 7     cout << myqueue.front();    return 0;}
Output:  
3
 
This function is used to reference the last or the newest element of the queue container. This function can be used to fetch the first element from the back of a queue.Syntax :  
queuename.back()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the last element of the queue container.
Examples:  
Input  :  myqueue = 1, 2, 3
          myqueue.back();
Output :  3

Input  :  myqueue = 3, 4, 1, 7, 3
          myqueue.back();
Output :  3
Errors and Exceptions  
If the queue container is empty, it causes undefined behaviorIt has a no exception throw guarantee if the queue is not empty
If the queue container is empty, it causes undefined behavior
It has a no exception throw guarantee if the queue is not empty
 
CPP
// CPP program to illustrate// Implementation of back() function#include <iostream>#include <queue>using namespace std; int main(){    queue<int> myqueue;    myqueue.push(3);    myqueue.push(4);    myqueue.push(1);    myqueue.push(7);     // Queue becomes 3, 4, 1, 7     cout << myqueue.back();    return 0;}
Output:  
7
Application : Given an empty queue of integers, add numbers to the queue, then print the difference between the first and the last element.  
Input  : 1, 2, 3, 4, 5, 6, 7, 8
Output : 7
Explanation - Last element = 8, First element = 1, Difference = 7
Algorithm 1. Add numbers to the queue using push() function 2. Compare the first and the last element. 3. If first element is larger, subtract last element from it and print it. 4. Else subtract first element from the last element and print it. 
CPP
// CPP program to illustrate// application Of front() and back() function#include <iostream>#include <queue>using namespace std; int main(){    queue<int> myqueue;    myqueue.push(8);    myqueue.push(7);    myqueue.push(6);    myqueue.push(5);    myqueue.push(4);    myqueue.push(3);    myqueue.push(2);    myqueue.push(1);     // Queue becomes 1, 2, 3, 4, 5, 6, 7, 8     if (myqueue.front() > myqueue.back()) {        cout << myqueue.front() - myqueue.back();    }    else if (myqueue.front() < myqueue.back()) {        cout << myqueue.back() - myqueue.front();    }    else        cout << "0";}
Output:  
7
 Let us see the differences in a tabular form -:
Its syntax is -:
front();
Its syntax is -:
back();
DivyaBalakrishnan
hritikbhatnagar2182
mayank007rawa
cpp-containers-library
CPP-Library
cpp-queue
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bitwise Operators in C/C++
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++
unordered_map in C++ STL
Substring in C++
Object Oriented Programming in C++
The C++ Standard Template Library (STL)
Inheritance in C++
C++ Classes and Objects
Sorting a vector in C++