The Wayback Machine - https://web.archive.org/web/20241108104723/https://www.geeksforgeeks.org/queuefront-queueback-c-stl/
Open In App

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

Last Updated : 23 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.
 

queue::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 
 

  1. If the queue container is empty, it causes undefined behavior
  2. 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

 

queue::back()

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 
 

  1. If the queue container is empty, it causes undefined behavior
  2. 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 -:

  queue::front()  queue::back()
1. It is used to return a reference to the next element in the queue. It is used to return a reference to the last element in the queue.
2.

Its syntax is -:

front();

Its syntax is -:

back();

3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. It is defined in <queue> header file. It is defined in <queue> header file.


Previous Article
Next Article

Similar Reads

list::front() and list::back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::front() This function is used to reference the first element of the list container. This func
3 min read
array::front() and array::back() in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be used to fetch the first element of an array. Synt
3 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
vector::front() and vector::back() in C++ STL
The vector::front() and vector::back() function in C++ STL are used to fetch the first and the last element of the std::vector container respectively. They are the member function of std::vector class and defined inside <vector> header file. In this article, we will learn about the vector::front() and vector::back() and how they are different
2 min read
queue::empty() and queue::size() in C++ STL
Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis function does not accept any parameter.Return Val
4 min read
list back() function in C++ STL
The list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element. Syntaxlist_name.back();ParametersThis function does not accept any parameter.Return ValueReturns a direct reference t
1 min read
forward_list::front() and forward_list::empty() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps tr
3 min read
list front() function in C++ STL
The list::front() is a built-in function in C++ STL which is used to return a reference to the first element in a list container. Unlike the list::begin() function, this function returns a direct reference to the first element in the list container. Syntaxlist_name.front(); Parameters This function does not accept any parameter, it simply returns a
1 min read
queue push() and pop() in C++ STL
The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file. In this article we will learn how to use queue::push() and qu
2 min read
Difference Between Linear Queue and Circular Queue
Queues are fundamental data structures in computer science that follow the First-In-First-Out (FIFO) principle. Among the various types of queues, linear queues and circular queues are commonly used. While they share some similarities, they differ significantly in structure and operational efficiency. This article explores the concepts, advantages,
4 min read
queue::swap() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::swap() swap() function is used to exchange the contents of two queu
2 min read
queue::emplace() in C++ STL
Queue is also an abstract data type or a linear data structure, which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). In a FIFO data structure, the first element added to the queue will be the first one to be removed. queue::emplace() This function is used to insert a new element into the qu
3 min read
C++ STL - Reverse Priority Queue
A priority Queue is an abstract data type that resembles a queue, and each element has a corresponding priority value. Priority queues are built on the top of the max heap and use an array or vector as an internal structure. In C++ STL, by default, the first element of the queue is the greatest of all elements in the queue and elements are in nonin
2 min read
Priority Queue in C++ Standard Template Library (STL)
A C++ priority queue is a type of container adapter, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue, and elements are in non-increasing or non-decreasing order (hence we can see that each element of the queue has a priority {fixed order}). In C++ STL, the top elemen
11 min read
Queue in C++ Standard Template Library (STL)
Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. Queues use an encapsulated object of deque or list (sequential container class) as its underlying container, providing a specific set of member functions to access its eleme
4 min read
Advantages of circular queue over linear queue
Linear Queue: A Linear Queue is generally referred to as Queue. It is a linear data structure that follows the FIFO (First In First Out) order. A real-life example of a queue is any queue of customers waiting to buy a product from a shop where the customer that came first is served first. In Queue all deletions (dequeue) are made at the front and a
3 min read
Sum of shortest distance on source to destination and back having at least a common vertex
Given a directed weighted graph and the source and destination vertex. The task is to find the sum of shortest distance on the path going from source to destination and then from destination to source such that both the paths have at least a common vertex other than the source and the destination. Note: On going from destination to source, all the
15+ min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
std::string::back() in C++ with Examples
This function returns a direct reference to the last character of the string. This shall only be used on non-empty strings. This can be used to access the last character of the string as well as to append a character at the end of the string. Length of the string remains unchanged after appending a character, last character of string gets replaced
2 min read
std::string::front() in C++with Examples
It is used to access the first character from the string. It returns a reference to the first character of the string. Unlike member string::begin, which returns an iterator to this same character, this function returns a direct reference. Syntax: string str ("GeeksforGeeks"); Accessing first character char first_char = str.front(); Inserting chara
2 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
list::push_front() and list::push_back() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::push_front() push_front() function is used to push elements into a list from the front. The n
4 min read
list::pop_front() and list::pop_back() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::pop_front() pop_front() function is used to pop or remove elements from a list from the front
4 min read
list::empty() and list::size() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::empty()empty() function is used to check if the list container is empty or not. Syntax : list
3 min read
list::remove() and list::remove_if() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::remove() remove() function is used to remove all the values from the list that correspond to
3 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
forward_list::push_front() and forward_list::pop_front() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps tr
4 min read
priority_queue::empty() and priority_queue::size() in C++ STL
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the smallest element at the top by simply passing an extr
4 min read
std::istream_iterator and std::ostream_iterator in C++ STL
The STL is a very powerful library in C++. It is strongly built on the principles of template programming. The STL library has three main components : Containers: These classes define the data structures which are used to contain the data. The data may be stored in linked lists, or trees or arrays. The containers provided in the STL are vector, deq
6 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
Practice Tags :
three90RightbarBannerImg