Queue Back Function

C++ Queue function returns the value of the last element of the queue. Here last element is the one that is newest. The element which was most recently added is returned.

Syntax

Example

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

Parameters

The function does not take any parameters. It is only used to return the value of the last element.

Return value

The function returns the last element of 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 accesses the container. For returning the last element the whole queue container is accessed and then the value of the newest element is given.

Exception Safety

Guarantee as equivalent to the operations that are performed on the underlying container object is provided.

Input Required

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