The following table shows the commonly used queue APIs:
|
API |
Description |
|
push() |
This appends a new value at the back of the queue |
|
pop() |
This removes the value at the front of the queue |
|
front() |
This returns the value in the front of the queue |
|
back() |
This returns the value at the back of the queue |
|
empty() |
This returns true when the queue is empty; otherwise it returns false |
|
size() |
This returns the number of values stored in the queue |
Let's use a queue in the following program:
#include <iostream>
#include <queue>
#include <iterator>
#include <algorithm>
using namespace std;
int main () {
queue<int> q;
q.push ( 100 );
q.push ( 200 );
q.push ( 300 );
cout << "\nValues in Queue are ..." << endl;
while ( ! q.empty() ) {
cout << q.front() << endl;
q.pop();
}
return 0;
}
The program can be compiled and the output can be viewed with the following commands:
g++ main.cpp -std=c++17
./a.out
The output of the program is as follows:
Values in Queue are ...
100
200
300
From the preceding output, you can observe that the values were popped out in the same sequence that they were pushed in, that is, FIFO.