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

Queue Back Function

BLUF: Mastering Queue Back 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 Back Function

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

The C++ Queue function retrieves the value of the latest element in the queue, where the newest element, i.e., the one added most recently, is the element returned.

Syntax

Example

value_type& back();
const value_type& back() const;

Parameters

The function does not require any arguments and is solely utilized to retrieve the value of the final element.

Return value

The function retrieves the final element from the queue.

Example

Example

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

Output:

Output

newqueue.back() is modified to 104

Complexity

The complexity of the function is constant.

Data races

The function retrieves data from the container. To obtain the final element, it accesses the entire queue container to retrieve the value of the most recent element.

Exception Safety

A promise is given that matches the actions carried out 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