C++ queue pop() Function
Example
Print and remove the elements of a queue, one by one:
queue<string> cars;
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
while (!cars.empty()) {
cout << cars.front() << "\n";
cars.pop();
}
Definition and Usage
The pop() function removes the first element of a queue. This is the element that was added first.
It does not return the removed element. Use front() first if you need its value. In the example, the loop runs until the queue is empty, so the elements are printed in the same order as they were added.
The pop function is defined in the <queue> header file.
Note: Do not call pop() on an empty queue. Check with empty() first.
Syntax
queue.pop();
Parameter Values
None.
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about queues in our Queues Tutorial.