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