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

C++ Usage and example of queue front() function

C++ STL Queue (Queue)

C ++ The Queue front() function returns the first element of the queue. The first element is the earliest element or the element added to the queue first. This function is used to return this element.

Syntax

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

Parameter

This function does not take any parameters and is used only to return the value of the first element in the queue.

Return value

This function returns the front element of the queue.

Instance1

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

Output:

Modify newqueue.front() to 44

Instance2

#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:

11

Complex

The complexity of the function is constant.

Data RACE

This function accesses the container. It accesses the queue container entirely and then returns the earliest element.

Exception Safety

Guarantees operations equivalent to those performed on underlying container objects.

C++ STL Queue (Queue)