Declaring queue in c++
Categories:
Declaring and Using Queues in C++ STL
Explore the std::queue
container adapter in C++ STL, understanding its declaration, essential operations, and practical applications for FIFO data structures.
The std::queue
is a container adapter that provides a first-in, first-out (FIFO) data structure. It's part of the C++ Standard Template Library (STL) and offers a convenient way to manage elements where the first element added is always the first one to be removed. This article will guide you through declaring, initializing, and performing common operations on std::queue
.
Understanding std::queue
Basics
A queue conceptually operates like a real-world waiting line. Elements are added to the back (enqueue operation) and removed from the front (dequeue operation). The std::queue
adapter by default uses std::deque
as its underlying container, but it can also be configured to use std::list
. It provides a restricted interface, exposing only the necessary operations for queue semantics, ensuring FIFO behavior.
#include <iostream>
#include <queue>
int main() {
// Declare a queue of integers
std::queue<int> myQueue;
// Add elements to the queue
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
// Access the front element
std::cout << "Front element: " << myQueue.front() << std::endl; // Output: 10
// Remove the front element
myQueue.pop();
std::cout << "Front element after pop: " << myQueue.front() << std::endl; // Output: 20
// Check if the queue is empty
if (myQueue.empty()) {
std::cout << "Queue is empty." << std::endl;
} else {
std::cout << "Queue is not empty. Size: " << myQueue.size() << std::endl; // Output: 2
}
return 0;
}
Demonstrates declaring a std::queue
, adding elements with push()
, accessing the front with front()
, removing with pop()
, and checking emptiness with empty()
.
myQueue.empty()
before calling myQueue.front()
or myQueue.pop()
to prevent undefined behavior when the queue is empty.Advanced Queue Operations and Customization
While the default std::queue
is sufficient for most cases, you can customize its underlying container. For instance, if you prefer std::list
over std::deque
(which is the default), you can specify it during declaration. Additionally, std::queue
provides methods like back()
to inspect the last element added without removing it, and size()
to get the current number of elements.
#include <iostream>
#include <queue>
#include <list>
int main() {
// Declare a queue using std::list as the underlying container
std::queue<std::string, std::list<std::string>> myStringQueue;
myStringQueue.push("Apple");
myStringQueue.push("Banana");
myStringQueue.push("Cherry");
std::cout << "Front element: " << myStringQueue.front() << std::endl; // Output: Apple
std::cout << "Back element: " << myStringQueue.back() << std::endl; // Output: Cherry
myStringQueue.pop();
std::cout << "Front element after pop: " << myStringQueue.front() << std::endl; // Output: Banana
std::cout << "Current size: " << myStringQueue.size() << std::endl; // Output: 2
return 0;
}
Shows how to declare a std::queue
with std::list
as its internal storage and uses back()
to peek at the last element.
Conceptual flow of queue operations (Enqueue and Dequeue).
When to Use std::queue
std::queue
is ideal for scenarios requiring strict FIFO ordering. Common use cases include: task scheduling (processing tasks in the order they arrive), breadth-first search (BFS) algorithms, managing requests in a server, or buffering data streams. Its simplicity and adherence to the FIFO principle make it a straightforward choice for these patterns.
1. Step 1
Include the <queue>
header at the top of your C++ file to access std::queue
.
2. Step 2
Declare a queue by specifying the element type, for example, std::queue<int> myQueue;
.
3. Step 3
Use myQueue.push(value);
to add elements to the back of the queue.
4. Step 4
Use myQueue.front();
to access the element at the front of the queue without removing it.
5. Step 5
Use myQueue.pop();
to remove the element from the front of the queue.
6. Step 6
Always check myQueue.empty();
before accessing or popping elements to avoid runtime errors.