Day 25 C++ queue container (queue)

The basic concept of queue

definition

In C++, a queue (queue) is a common data structure that adopts the principle of First-In-First-Out (FIFO), and it has two exits. The characteristic of the queue is that it only allows insertion at one end (tail) and deletion at the other end (head) . In the queue, only the earliest added elements can be accessed and manipulated, while subsequent added elements need to wait until the previous elements have been processed before they can be accessed.

insert image description here

Notice

In the C++ standard library, std::queuequeues can be implemented using template classes. The header file needs to be introduced. <queue>
The queue container allows adding elements from one end and removing elements from the other end.
Only the head and tail of the queue can be used by the outside world, so the queue does not allow traversal behavior

std::queueQueue( ) in the C++ standard library does not provide direct indexing functionality by itself . Since the queue is a first-in-first-out data structure, its insertion operation can only be performed at the end of the queue, and its deletion operation can only be performed at the head of the queue. Elements cannot be accessed through indexes like arrays or vectors.
To implement a queue with an index function, other data structures can be used to assist the implementation, such as vector ( std::vector) or linked list ( std::list)

basic concept

Front - Points to the position of the earliest added element in the queue.
Rear - Points to the position of the last element added to the queue.
Enqueue - add elements to the end of the queue.
Dequeue - Removes elements from the head of the queue.
Empty - When there are no elements in the queue, it is called empty.
Queue size (Size) - indicates the number of elements in the queue.

Queue common interface

Constructor

  • queue<T> que;The queue is implemented by a template class, and the default construction form of the queue object
  • queue(const queue &que);copy constructor

assignment operation

  • queue& operator=(const queue &que);overloaded equals operator

data access

  • push(elem);Add an element to the end of the queue
  • pop();Remove the first element from the head of the queue
  • back();returns the last element
  • front(); returns the first element

size operation

  • empty();Check if the stack is empty
  • size(); Return the size of the stack

example

the code

#include <iostream>
#include <queue>

int main() {
    
    
    std::queue<int> que;
    
    // 往队尾添加元素
    que.push(10);
    que.push(20);
    que.push(30);

    // 返回第一个元素
    std::cout << "队头元素为:" << que.front() << std::endl;

    // 返回最后一个元素
    std::cout << "队尾元素为:" << que.back() << std::endl;

    // 移除第一个元素
    que.pop();

    // 再次返回第一个元素
    std::cout << "队头元素为:" << que.front() << std::endl;

    // 获取队列的大小
    std::cout << "队列的大小为:" << que.size() << std::endl;

    // 判断队列是否为空
    if (que.empty()) {
    
    
        std::cout << "队列为空" << std::endl;
    }
    return 0;
}

The result of the operation is as follows:

The element at the head of the queue is: 10
The element at the end of the queue is: 30
The element at the head of the queue is: 20
The size of the queue is: 2

analyze

This example creates an integer queue object std::queue que
and then adds elements 10, 20 and 30 to the end of the queue through the push(elem) method.
Use the front() method to get the value of the first element of the queue
Use the back() method to get the value of the last element of the queue,
then use the pop() method to remove the first element of the queue,
and finally use the size() method to return the size of the queue.
Use the empty() method to determine whether the queue is empty.

Summarize

  • Enqueue — push
  • dequeue — pop
  • Returns the front element of the queue — front
  • Return the element at the end of the queue — back
  • Determine whether the team is empty—empty
  • Returns the queue size — size

queue application scenario

task scheduling

In a multitasking system, queues can be used to manage pending tasks. When a new task arrives, it is added to the end of the queue; when the task is executed, the task is taken out from the head of the queue for processing. In this way, tasks can be executed sequentially in the order of arrival, and fairness can be guaranteed.

messaging

In a message queuing system, messages can be delivered using queues. Producers put messages at the end of the queue, and consumers take messages from the head of the queue for processing. This enables asynchronous communication, improving the scalability and reliability of the system.

buffer zone

During data processing, queues can be used as buffers to balance the speed difference between producers and consumers. Producers can put data into the queue, and consumers take data from the queue for processing. In this way, direct interaction between producers and consumers can be avoided, and the performance and stability of the system can be improved.

Network request handling

In server-side applications, queues can be used to process requests from clients. When a new request arrives, put it at the end of the queue, and then process it sequentially in the order of the queue. This can avoid excessive system load caused by processing a large number of requests at the same time, and improve the response speed of the system at the same time.

Thread pool task scheduling

In multithreaded programming, queues can be used to manage pending tasks. Put the task at the end of the queue, and the thread in the thread pool will take the task out from the head of the queue for execution. This can avoid resource competition and performance degradation caused by too many threads, and achieve orderly execution of tasks.

Guess you like

Origin blog.csdn.net/m0_74921567/article/details/132182482