C++ queue front() Function
Example
Get the first element of a queue:
queue<string> cars;
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");
cout << cars.front();
Definition and Usage
The front() function returns the first element of a queue. This is the element that was added first, and the next one to be removed by pop().
You can also use it to change the first element, like cars.front() = "Tesla";.
The front function is defined in the <queue> header file.
Note: Do not call front() on an empty queue. Check with empty() first.
Syntax
queue.front();
Parameter Values
None.
Technical Details
| Returns: | A reference to the first element of the queue. |
|---|
Related Pages
Read more about queues in our Queues Tutorial.