C++ Queues
In C++, a queue is a container adapter that provides a First-In-First-Out (FIFO) data structure. It allows for adding elements to the back of the queue and removing elements from the front. Queues are particularly useful for scenarios where you need to process elements in the order they were added.
Key Features of std::queue
- FIFO Order: The first element added is the first one to be removed.
- Simple Interface: Supports basic operations like enqueue (add), dequeue (remove), and access to the front and back elements.
- Underlying Container: By default,
std::queueusesstd::dequeas its underlying container, but other containers likestd::listcan also be used.
Basic Syntax
To use std::queue, include the header and use the std::queue template class:
cpp#include
Basic Operations
1. Declaration
cppstd::queue<int> q; // Creates an empty queue of integers
2. Adding Elements
- Enqueue: Add an element to the back of the queue using
push().
cppq.push(10); // Add 10 to the back of the queue
q.push(20); // Add 20 to the back of the queue
3. Removing Elements
- Dequeue: Remove the element from the front of the queue using
pop(). Note thatpop()does not return the removed element; it simply removes it.
cppq.pop(); // Removes the element from the front (10 in this case)
4. Accessing Elements
- Front: Access the front element of the queue using
front(). - Back: Access the back element of the queue using
back().
cppint frontElement = q.front(); // Access the front element (20)
int backElement = q.back(); // Access the back element (20)
5. Size and Capacity
- Size: Returns the number of elements in the queue using
size(). - Empty: Checks if the queue is empty using
empty().
cppstd::size_t size = q.size(); // Get the number of elements
bool isEmpty = q.empty(); // Check if the queue is empty
Example Code
Here’s a complete example demonstrating these operations:
cpp#include
#include
int main() {
std::queue<int> q;
// Adding elements
q.push(10);
q.push(20);
q.push(30);
// Accessing and removing elements
std::cout << "Front element: " << q.front() << std::endl; // 10
std::cout << "Back element: " << q.back() << std::endl; // 30
q.pop(); // Removes 10
std::cout << "New front element: " << q.front() << std::endl; // 20
// Checking size and empty status
std::cout << "Queue size: " << q.size() << std::endl; // 2
std::cout << "Is queue empty? " << (q.empty() ? "Yes" : "No") << std::endl; // No
// Removing remaining elements
q.pop(); // Removes 20
q.pop(); // Removes 30
std::cout << "Queue size after popping all elements: " << q.size() << std::endl; // 0
return 0;
}
Underlying Container
By default, std::queue uses std::deque as its underlying container. You can also specify a different container that supports the required operations, such as std::list.
Custom Container Example
Using std::list as the underlying container:
cpp#include
#include
#include
int main() {
std::queue<int, std::list<int>> q;
// Adding elements
q.push(10);
q.push(20);
q.push(30);
// Accessing and removing elements
std::cout << "Front element: " << q.front() << std::endl; // 10
q.pop(); // Removes 10
std::cout << "New front element: " << q.front() << std::endl; // 20
return 0;
}
Key Points
- FIFO Order: Elements are accessed and removed in the order they were added.
- Limited Operations: Provides a restricted set of operations focused on queue functionality.
- Custom Containers: You can use different underlying containers by specifying them as template parameters.
std::queue is ideal for scenarios where you need to manage and process elements in the order they were inserted, such as in scheduling tasks or handling events.