C++ queue
πΆ C++ queue (FIFO Data Structure)
std::queue is a container adapter in the C++ STL that follows the FIFO (First In, First Out) principle.
Elements are inserted at the back and removed from the front.
πΉ 1. Why Use Queue?
Task scheduling
Printer queues
CPU/process scheduling
BFS (Breadth-First Search)
Customer service systems
πΉ 2. Include Header
πΉ 3. Declaring a Queue
With underlying container:
πΉ 4. Basic Queue Operations
πΉ 5. Size and Empty Check
πΉ 6. Traversing a Queue
β No iterators
β Use a copy:
πΉ 7. Queue Example: Simple Simulation
πΉ 8. Queue Example: BFS (Graph)
πΉ 9. Deque vs Queue
| Feature | queue | deque |
|---|---|---|
| Access | Front & Back | Front, Back, Random |
| Insert | Back only | Both ends |
| Remove | Front only | Both ends |
πΉ 10. Queue Time Complexity
| Operation | Complexity |
|---|---|
| push | O(1) |
| pop | O(1) |
| front/back | O(1) |
β Common Mistakes
β Always check:
πΉ 11. queue vs priority_queue
| queue | priority_queue |
|---|---|
| FIFO | Highest priority first |
| Order preserved | Sorted by priority |
| BFS | Dijkstra, scheduling |
π Summary
std::queuefollows FIFOInsert at back, remove from front
No iterators
Built on
dequeby defaultUsed in scheduling and BFS
