Share about priority_queue in c++

redlion827

New member
## Giới thiệu về ưu tiên_queue trong C ++

Hàng đợi ưu tiên là một cấu trúc dữ liệu duy trì một tập hợp các phần tử theo thứ tự được sắp xếp, trong đó ưu tiên ** ** của một phần tử được sử dụng để xác định vị trí của nó trong hàng đợi.Việc triển khai phổ biến nhất của hàng đợi ưu tiên là ** heap **, là cấu trúc dữ liệu dựa trên cây trong đó trẻ em của mỗi nút được đặt hàng theo một cách cụ thể.

Trong C ++, lớp `priority_queue` được cung cấp bởi thư viện tiêu chuẩn.Lớp `Priority_queue` có thể được sử dụng để lưu trữ các phần tử thuộc bất kỳ loại nào có thể được so sánh với nhau.Các yếu tố của hàng đợi ưu tiên được ** đẩy ** lên hàng đợi theo bất kỳ thứ tự nào, và chúng được ** xuất hiện ** từ hàng đợi theo thứ tự giảm ưu tiên.

Mã sau đây cho thấy cách tạo hàng đợi ưu tiên của các số nguyên và đẩy một số phần tử lên hàng đợi:

`` `C ++
#include <Istream>
#include <Verue>

sử dụng không gian tên STD;

int main () {
// Tạo một hàng đợi ưu tiên của số nguyên.
Ưu tiên_Queue <Int> PQ;

// đẩy một số yếu tố lên hàng đợi.
pq.push (10);
pq.push (5);
pq.push (15);

// In các yếu tố của hàng đợi.
while (! pq.empty ()) {
cout << pq.top () << endl;
pq.pop ();
}

trả lại 0;
}
`` `

Đầu ra của mã trên là:

`` `
15
10
5
`` `

## hashtags

* #hàng đợi ưu tiên
* #C ++
* #cấu trúc dữ liệu
* #Heap
* #Sorting
=======================================
## About Priority_Queue in C++

A priority queue is a data structure that maintains a set of elements in sorted order, where the **priority** of an element is used to determine its position in the queue. The most common implementation of a priority queue is a **heap**, which is a tree-based data structure in which the children of each node are ordered in a specific way.

In C++, the `priority_queue` class is provided by the standard library. The `priority_queue` class can be used to store elements of any type that can be compared with each other. The elements of a priority queue are **pushed** onto the queue in any order, and they are **popped** from the queue in order of decreasing priority.

The following code shows how to create a priority queue of integers and push some elements onto the queue:

```c++
#include <iostream>
#include <queue>

using namespace std;

int main() {
// Create a priority queue of integers.
priority_queue<int> pq;

// Push some elements onto the queue.
pq.push(10);
pq.push(5);
pq.push(15);

// Print the elements of the queue.
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}

return 0;
}
```

The output of the above code is:

```
15
10
5
```

## Hashtags

* #priority_queue
* #C++
* #data_structures
* #Heap
* #Sorting
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top