Queue Front Function - C++ Programming Tutorial
C++ Course / Data Structures / Queue Front Function

Queue Front Function

BLUF: Mastering Queue Front Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Queue Front Function

C++ is renowned for its efficiency. Learn how Queue Front Function enables low-level control and high-performance computing in the tutorial below.

The front function in C++ Queue retrieves the value of the front element in the queue, which is the earliest element added or the first element in the queue. It is specifically designed to provide access to this particular element.

Syntax

Example

value_type& front();
const value_type& front() const;

Parameters

The function doesn't accept any parameters; its sole purpose is to retrieve the value of the oldest element, which is the element at the beginning of the queue.

Return value

The function retrieves the initial element from the queue.

Example 1

Example

#include <iostream>
#include <queue>
int main()
{
	std::queue<int> newqueue;
	newqueue.push(24);
	newqueue.push(80);
	newqueue.front () +=20;
	std::cout <<"newqueue.front() is modified to " << newqueue.front();
	return 0;
}

Output:

Output

newqueue.front() is modified to 44

Example 2

Example

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	queue<int> newqueue;
	newqueue.push(11);
	newqueue.push(22);
	newqueue.push(33);
	cout << newqueue.front();
	return 0;
}

Output:

Complexity

The complexity of the function is constant.

Data races

The function retrieves data from the container by accessing the queue container in its entirety, then returning the oldest element within it.

Exception Safety

A promise is given to match the actions executed on the container object.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience