Introduction: Why Do You Need Task Queues in C++?
\nImagine you are standing in line at a supermarket checkout. The first person to arrive is served first. In programming, this structure is called a queue. It is one of the fundamental data types that operates on the FIFO (First In, First Out) principle.
\nIn C++, task queues are used everywhere: from processing network requests to managing threads in multithreaded applications. For a beginner programmer, understanding queues is a step towards mastering more complex topics such as asynchronous programming and design patterns.
\nIn this article, we will break down how to create and use queues in C++, look at the standard container std::queue, write several practical examples, and discuss common beginner mistakes.
Queue Basics: Theory and First Example
\nWhat is a Queue?
\nA queue is an abstract data type that supports two main operations:
\n- \n
- Adding an element to the end of the queue (enqueue / push); \n
- Extracting an element from the front of the queue (dequeue / pop). \n
In C++, the standard queue is implemented in the header file <queue> as the class std::queue. By default, it uses std::deque as the underlying container, but you can replace it with std::list.
First Example: A Queue of Numbers
\nLet's write a simple program that creates a queue of integers, adds several values to it, and prints them to the screen.
\n#include <iostream>\n#include <queue>\n\nint main() {\n std::queue<int> my_queue;\n\n // Adding elements to the queue\n my_queue.push(10);\n my_queue.push(20);\n my_queue.push(30);\n\n std::cout << "First element (front): " << my_queue.front() << std::endl;\n std::cout << "Last element (back): " << my_queue.back() << std::endl;\n std::cout << "Queue size: " << my_queue.size() << std::endl;\n\n // Extracting elements\n while (!my_queue.empty()) {\n std::cout << "Extracted: " << my_queue.front() << std::endl;\n my_queue.pop();\n }\n\n return 0;\n}\nProgram output:
\nFirst element (front): 10\nLast element (back): 30\nQueue size: 3\nExtracted: 10\nExtracted: 20\nExtracted: 30\nNote: front() returns the first element (which will be extracted next), and back() returns the last added element. The pop() method removes the element but does not return it — this is important to remember!
Basic Methods of std::queue
\nTo work efficiently with a queue, you need to know all its methods. Here is the complete list:
\n- \n
push(const T& value)— adds an element to the end of the queue; \n pop()— removes the first element (does not return it); \n front()— returns a reference to the first element; \n back()— returns a reference to the last element; \n empty()— checks if the queue is empty (returns true/false); \n size()— returns the number of elements. \n
Important: Before calling front() or pop(), always check if the queue is empty using empty(). Attempting to access elements of an empty queue will lead to undefined behavior (most likely, a program crash).
Practical Example: A Task Queue
\nNow let's simulate a real-world scenario. Suppose we have a system that processes orders. Each order is a structure with an ID and a description. We place the orders into a queue