Share c++ stl queue source code

lephuonglam.vu

New member
### C ++ mã nguồn hàng đợi STL

#### Hàng đợi là gì?

Hàng đợi là một cấu trúc dữ liệu tuyến tính trong đó các phần tử được thêm vào một đầu (được gọi là phía sau) và được loại bỏ khỏi đầu kia (được gọi là mặt trước).Hàng đợi thường được sử dụng để thực hiện các cấu trúc dữ liệu FIFO (trước, trước).

#### Làm thế nào để tạo một hàng đợi trong C ++ STL?

Để tạo hàng đợi trong C ++ STL, bạn có thể sử dụng lớp `Queue`.Lớp `Queue` là một lớp mẫu lấy loại phần tử trong hàng đợi làm tham số mẫu của nó.

Để tạo hàng đợi, bạn có thể sử dụng cú pháp sau:

`` `C ++
Hàng đợi <Int> my_queue;
`` `

Điều này sẽ tạo ra một hàng đợi các số nguyên.

#### Làm thế nào để thêm các phần tử vào hàng đợi trong C ++ STL?

Để thêm một phần tử vào hàng đợi, bạn có thể sử dụng phương thức `push`.Phương thức `push` có một đối số duy nhất, đó là phần tử được thêm vào hàng đợi.

Để thêm một phần tử vào hàng đợi, bạn có thể sử dụng cú pháp sau:

`` `C ++
my_queue.push (10);
`` `

Điều này sẽ thêm số nguyên 10 vào hàng đợi.

#### Làm thế nào để loại bỏ các phần tử khỏi hàng đợi trong C ++ STL?

Để loại bỏ một phần tử khỏi hàng đợi, bạn có thể sử dụng phương thức `pop`.Phương thức `pop` không lấy bất kỳ đối số nào.

Để xóa một phần tử khỏi hàng đợi, bạn có thể sử dụng cú pháp sau:

`` `C ++
phần tử int = my_queue.pop ();
`` `

Điều này sẽ loại bỏ phần tử ở phía trước của hàng đợi và trả lại nó.

#### Ví dụ về việc sử dụng hàng đợi trong C ++ STL

Sau đây là một ví dụ về việc sử dụng hàng đợi trong C ++ STL để thực hiện một chương trình đơn giản in 10 số nguyên tố đầu tiên:

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

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

int main () {
// Tạo một hàng đợi số nguyên.
Hàng đợi <Int> my_queue;

// Thêm 10 số nguyên tố đầu tiên vào hàng đợi.
for (int i = 2; i <= 20; i ++) {
if (isprime (i)) {
my_queue.push (i);
}
}

// In 10 số nguyên tố đầu tiên từ hàng đợi.
while (! my_queue.empty ()) {
cout << my_queue.front () << endl;
my_queue.pop ();
}

trả lại 0;
}

// Chức năng để kiểm tra xem một số là số nguyên tố.
bool isprime (int n) {
for (int i = 2; i <= sqrt (n); i ++) {
if (n % i == 0) {
trả lại sai;
}
}

trả lại đúng;
}
`` `

#### hashtags

* C ++
* STL
* xếp hàng
* cấu trúc dữ liệu
* vào trước ra trước
=======================================
### C++ STL Queue Source Code

#### What is a queue?

A queue is a linear data structure in which elements are added to one end (called the rear) and removed from the other end (called the front). Queues are often used to implement FIFO (first-in, first-out) data structures.

#### How to create a queue in C++ STL?

To create a queue in C++ STL, you can use the `queue` class. The `queue` class is a template class that takes the type of the elements in the queue as its template parameter.

To create a queue, you can use the following syntax:

```c++
queue<int> my_queue;
```

This will create a queue of integers.

#### How to add elements to a queue in C++ STL?

To add an element to a queue, you can use the `push` method. The `push` method takes a single argument, which is the element to be added to the queue.

To add an element to a queue, you can use the following syntax:

```c++
my_queue.push(10);
```

This will add the integer 10 to the queue.

#### How to remove elements from a queue in C++ STL?

To remove an element from a queue, you can use the `pop` method. The `pop` method does not take any arguments.

To remove an element from a queue, you can use the following syntax:

```c++
int element = my_queue.pop();
```

This will remove the element at the front of the queue and return it.

#### Example of using a queue in C++ STL

The following is an example of using a queue in C++ STL to implement a simple program that prints the first 10 prime numbers:

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

using namespace std;

int main() {
// Create a queue of integers.
queue<int> my_queue;

// Add the first 10 prime numbers to the queue.
for (int i = 2; i <= 20; i++) {
if (isPrime(i)) {
my_queue.push(i);
}
}

// Print the first 10 prime numbers from the queue.
while (!my_queue.empty()) {
cout << my_queue.front() << endl;
my_queue.pop();
}

return 0;
}

// Function to check if a number is prime.
bool isPrime(int n) {
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}

return true;
}
```

#### Hashtags

* c++
* stl
* queue
* data structure
* first-in, first-out
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top