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
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
#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:
newqueue.front() is modified to 44
Example 2
#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.