C++ - C++ Queues Part 2: Basic Queue Operations
Queues allow adding, removing, and accessing elements in a controlled manner.
Examples
Accessing Front and Back Elements
std::cout << "Front: " << myQueue.front(); // Output: 10
std::cout << "Back: " << myQueue.back(); // Output: 30
Explanation: front() returns the first element, while back() returns the last element.
Removing Elements (pop())
myQueue.pop(); // Removes 10
std::cout << "New Front: " << myQueue.front(); // Output: 20
Explanation: pop() removes the first element from the queue.
Checking if Queue is Empty (empty())
while (!myQueue.empty()) {
std::cout << myQueue.front() << " ";
myQueue.pop();
}
// Output: 20 30
Explanation: empty() checks if there are elements left in the queue before popping.