English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and example of the Queue back() function

C++ STL Queue (Queue)

C ++The queue function returns the last element of the queue. Here, the last element is the most recent element. That is, it returns the last added element.

Syntax

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

Parameters

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

Return Value

This function returns the last element of the queue.

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() modified to" << newqueue.back();
	return 0;
}

Output:

Modify newqueue.back() to 104

Complex

The complexity of the function is constant.

Data Races

This function accesses the container. To return the last element, it accesses the entire queue container and then provides the value of the latest element.

Exception Safety

It provides guarantees equivalent to operations performed on underlying container objects.

C++ STL Queue (Queue)