How to Use the STL Queue Container in C++
-
Use
std::queue
to Declare an STL Queue Object in C++ -
Use the
size
Member Function to Retrieve the Size of the Queue in C++ -
Use the
swap
Member Function to Exchange Contents of Two Queue Objects in C++
This article explains how to utilize the STL queue container in C++.
Use std::queue
to Declare an STL Queue Object in C++
The C++ standard library provides several container classes used to provide fundamental types for managing collections of objects. Although, there are some special containers called container adapters
, which transform fundamental containers for special needs with the most restricted interface.
One of these container adapters is std::queue
, which adapts sequence containers to provide a FIFO (first-in-first-out) data structure. Note that the underlying fundamental container should implement SequenceContainer
requirements and have four member functions: back
, front
, push_back
, and pop_front
. Only the std::deque
and std::list
functions from the fundamental containers satisfy the above requirements and thus, can be used to store queue
elements.
#include <iostream>
#include <queue>
using std::cout;
using std::endl;
using std::queue;
using std::string;
template <typename Queue>
void printQueue(Queue q) {
while (!q.empty()) {
cout << q.front() << ", ";
q.pop();
}
cout << endl;
}
int main() {
std::queue<string> q1;
std::deque<string> deq1 = {"one", "eight", "six", "seven", "eleven", "ten"};
for (const auto& n : deq1) q1.push(n);
printQueue(q1);
std::queue<string> q2(deq1);
printQueue(q2);
return EXIT_SUCCESS;
}
Output:
one, eight, six, seven, eleven, ten,
one, eight, six, seven, eleven, ten,
The std::queue
container can be initialized using the existing std::deque
object. The constructor for the latter initialization only needs one argument of type deque
. On the other hand, we can declare a queue object and add elements using the push
function. In the above code snippet, we demonstrate the above methods and print the contents of the queues using the printQueue
function.
Notice that the iteration over the queue elements is done with the while
loop utilizing the empty
function because iterators are not accessible. In each loop cycle, we access the first element using the front
member function and use pop
to remove the same element. Since the latter operation modifies the original queue object, we accept it as the value argument rather than the reference.
Use the size
Member Function to Retrieve the Size of the Queue in C++
You can use the size
member function to retrieve the number of elements in the queue object. The function has a constant time performance.
#include <iostream>
#include <queue>
using std::cout;
using std::endl;
using std::queue;
using std::string;
int main() {
std::deque<string> deq1 = {"one", "eight", "six", "seven", "eleven", "ten"};
std::queue<string> q2(deq1);
cout << "size of the queue = " << q2.size() << endl;
return EXIT_SUCCESS;
}
Output:
size of the queue = 6
Use the swap
Member Function to Exchange Contents of Two Queue Objects in C++
We can swap the elements of two queue objects using the swap
member function. It takes another queue object as the only argument and exchanges its contents with the caller object. Note that the argument object assigned the contents of the caller queue.
#include <iostream>
#include <queue>
using std::cout;
using std::endl;
using std::queue;
using std::string;
template <typename Queue>
void printQueue(Queue q) {
while (!q.empty()) {
cout << q.front() << ", ";
q.pop();
}
cout << endl;
}
int main() {
std::queue<string> q1;
std::deque<string> deq1 = {"one", "eight", "six", "seven", "eleven", "ten"};
for (const auto& n : deq1) q1.push(n);
std::queue<string> q2(deq1);
q2.push(deq1.front());
q2.push("zero");
cout << "q1: ";
printQueue(q1);
cout << "q2: ";
printQueue(q2);
q1.swap(q2);
cout << "q1: ";
printQueue(q1);
cout << "q2: ";
printQueue(q2);
return EXIT_SUCCESS;
}
Output:
q1: one, eight, six, seven, eleven, ten,
q2: one, eight, six, seven, eleven, ten, one, zero,
q1: one, eight, six, seven, eleven, ten, one, zero,
q2: one, eight, six, seven, eleven, ten,
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook