

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
queue::front() and queue::back() in C++ STL
In this article we will be discussing the working, syntax and examples of queue::front() and queue::back() functions in C++ STL.
What is a queue in C++ STL?
Queue is a simple sequence or data structure defined in the C++ STL which does insertion and deletion of the data in FIFO(First In First Out) fashion. The data in a queue is stored in continuous manner. The elements are inserted at the end and removed from the starting of the queue. In C++ STL there is already a predefined template of queue, which inserts and removes the data in the similar fashion of a queue.
What is queue::front()?
queue::front() is an inbuilt function in C++ STL which is declared in
Like in the above given figure, head i.e. 1 is the first element which has been entered in the queue and tail i.e. -4 is the last or the most recent element which is entered in the queue
Syntax
myqueue.front();
This function accepts no parameter
Return value
This function returns a reference to the element which is first inserted in the queue container.
Example
Input: queue<int> myqueue = {10, 20, 30, 40}; myqueue.front(); Output: Front element of the queue = 10
Example
#include <iostream> #include <queue> using namespace std; int main(){ queue<int> Queue; Queue.push(10); Queue.push(20); Queue.push(30); Queue.push(40); Queue.push(40); cout<<"Element in front of a queue is: "<<Queue.front(); return 0; }
Output
If we run the above code it will generate the following output −
Element in front of a queue is: 10
What is queue::back()?
queue::back() is an inbuilt function in C++ STL which is declared in
Syntax
myqueue.back();
This function accepts no parameter
Return value
This function returns a reference to the element which is last inserted in the queue container.
Example
Input: queue<int> myqueue = {10, 20 30, 40}; myqueue.back(); Output: Back element of the queue = 40
Example
#include <iostream> #include <queue> using namespace std; int main(){ queue<int> Queue; Queue.push(10); Queue.push(20); Queue.push(30); Queue.push(40); Queue.push(50); cout<<"Elements at the back of the queue is: "<<Queue.back(); return 0; }
Output
If we run the above code it will generate the following output −
Elements at the back of the queue is: 50
- Related Questions & Answers
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- list::front() and list::back() in C++ STL
- queue::emplace() in C++ STL
- queue::swap() in C++ STL
- deque front( ) and deque back( ) in C++ in STL
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- Difference Between Linear Queue and Circular Queue
- Stack and Queue in Python using queue Module
- C++ Program to Implement Queue in STL
- Reversing the alphabet from back to front and front to back in JavaScript
- Priority Queue in C++ Standard Template Library (STL)
- STL Priority Queue for Structure or Class in C++
- Stack and Queue in C#
- Queue in Java