Home | Projects | Notes > C++ Programming > Container Adaptor - std::queue
std::queue
std::queue
Declared in the <queue>
header file.
First-In, First-Out (FIFO) data structure.
Implemented as an adaptor over other STL containers.
Can use list
, or deque
as the underlying container.
Queue methods work through delegation—they internally call the front()
, back()
, push_back()
, and pop_front()
functions of the underlying container.
Since vector
does not support pop_front()
due to inefficiency, it cannot be used as the underlying container for queue
.
Elements are pushed at the back and popped from the front.
Iterators are not supported.
Use cases:
OS schedulers
Inter-task/thread communication in multi-threaded systems
Breadth-First Search (BFS) in graphs
Real-time data streams / buffers in audio/video streaming, sensor data handling
Printer spoolers
Because std::queue
is a container adaptor, you have the flexibility to specify the underlying container—such as deque
or list
—at the time of queue creation.
31std::queue<int> q1; // deque (by default)
2std::queue<int, std::list<int>> q2; // list
3std::queue<int, std::deque<int>> q3; // deque
Note that
std::queue<int, std::vector<int>> q
will fail to compile becausestd::vector
lackspop_front()
.
For more information, see cppreference.com.
Operation | Behavior |
---|---|
push() | Insert an element at the back of the queue. |
pop() | Remove an element from the front of the queue. |
front() | Access the element at the front. |
back() | Access the element at the back. |
empty() | Is the queue empty? |
size() | Number of elements in the queue . |
std::queue
It's best to adhere to the fundamental operations of a queue. Introducing additional methods like insert()
compromises the integrity of the queue's intended behavior.
711
2
3
4// Prints the queue by repeatedly removing elements from the front.
5// Note that this function is passed a queue 'by value' so whatever happens
6// inside this function will not affect the original queue.
7template <typename T>
8void print(std::queue<T> q)
9{
10 std::cout << "[ ";
11 while (!q.empty())
12 {
13 T elem = q.front();
14 q.pop();
15 std::cout << elem << " ";
16 }
17 std::cout << "]" << std::endl;
18}
19
20// push(), pop(), empty(), size()
21void test(void)
22{
23 std::cout << "\nTEST" << std::endl;
24
25 std::queue<int> q;
26
27 for (int n : {1, 2, 3, 4, 5})
28 {
29 q.push(n);
30 }
31 print(q);
32
33 std::cout << "Front: " << q.front() << std::endl;
34 std::cout << "Back: " << q.back() << std::endl;
35
36 q.push(100);
37 print(q);
38
39 q.pop();
40 q.pop();
41 print(q);
42
43 while (!q.empty())
44 {
45 q.pop();
46 }
47 print(q);
48
49 std::cout << "Size: " << q.size() << std::endl;
50
51 q.push(10);
52 q.push(100);
53 q.push(1000);
54 print(q);
55
56 std::cout << "Front: " << q.front() << std::endl;
57 std::cout << "Back: " << q.back() << std::endl;
58
59 q.front() = 5;
60 q.back() = 5000;
61
62 print(q);
63 std::cout << "Front: " << q.front() << std::endl;
64 std::cout << "Back: " << q.back() << std::endl;
65}
66
67int main(int argc, char *argv[])
68{
69 test();
70 return 0;
71}
151
2TEST
3[ 1 2 3 4 5 ]
4Front: 1
5Back: 5
6[ 1 2 3 4 5 100 ]
7[ 3 4 5 100 ]
8[ ]
9Size: 0
10[ 10 100 1000 ]
11Front: 10
12Back: 1000
13[ 5 100 5000 ]
14Front: 5
15Back: 5000