Share set c++,

huulong768

New member
#C ++, #Set, #C ++ Set, #STL, #Container ** Đặt trong C ++ **

Một tập hợp là một cấu trúc dữ liệu lưu trữ các yếu tố duy nhất không theo thứ tự cụ thể.Đó là một bộ sưu tập ** chưa được đặt hàng ** và ** chưa được đăng ký **.Các yếu tố trong một tập hợp ** không được phép nhân đôi **.

Các bộ được triển khai bằng cách sử dụng ** cây đen đỏ ** trong Thư viện mẫu tiêu chuẩn C ++ (STL).Cây đen đỏ là một loại cây tìm kiếm nhị phân tự cân bằng, đảm bảo rằng các thuộc tính sau luôn luôn đúng:

*Cây là ** acyclic **, có nghĩa là không có chu kỳ trong cây.
*Cây được ** cân bằng **, có nghĩa là sự khác biệt về độ cao của các con trừ bên trái và bên phải của bất kỳ nút nào là nhiều nhất là một.
*Cây được ** đặt hàng **, có nghĩa là các phần tử trong cây được lưu trữ theo thứ tự tăng dần.

Bộ được sử dụng để lưu trữ một bộ sưu tập các yếu tố độc đáo.Chúng thường được sử dụng để kiểm tra sự tồn tại của một yếu tố trong một bộ sưu tập, để tìm sự kết hợp hoặc giao điểm của hai bộ hoặc để tìm các phần tử phổ biến cho hai bộ.

## Tạo một bộ

Để tạo một tập hợp, bạn có thể sử dụng mẫu lớp `std :: set`.Mẫu lớp `std :: set` lấy một tham số loại chỉ định loại thành phần mà tập hợp sẽ lưu trữ.Ví dụ: mã sau tạo một tập hợp lưu trữ số nguyên:

`` `C ++
STD :: Đặt <Int> my_set;
`` `

## Thêm phần tử vào một tập hợp

Bạn có thể thêm các phần tử vào một tập hợp bằng phương thức `chèn ()`.Phương thức `chèn ()` lấy một phần tử làm đối số của nó và chèn phần tử vào tập hợp.Nếu phần tử đã có trong tập hợp, phương thức `chèn ()` không làm gì cả.Ví dụ: mã sau đây thêm các số nguyên 1, 2 và 3 vào một tập hợp:

`` `C ++
my_set.insert (1);
my_set.insert (2);
my_set.insert (3);
`` `

## Xóa các phần tử khỏi một tập hợp

Bạn có thể xóa các phần tử khỏi một tập hợp bằng phương thức `erase ()`.Phương thức `erase ()` lấy một phần tử làm đối số của nó và loại bỏ phần tử khỏi tập hợp.Nếu phần tử không nằm trong tập hợp, phương thức `erase ()` không làm gì cả.Ví dụ: mã sau sẽ loại bỏ số nguyên 2 khỏi một tập hợp:

`` `C ++
my_set.erase (2);
`` `

## Tìm kiếm các thành phần trong một tập hợp

Bạn có thể tìm kiếm các phần tử trong một tập hợp bằng phương thức `find ()`.Phương thức `find ()` lấy một phần tử làm đối số của nó và trả về một con trỏ cho phần tử trong tập hợp nếu nó được tìm thấy.Nếu phần tử không được tìm thấy, phương thức `find ()` trả về `nullptr`.Ví dụ: mã sau kiểm tra xem số nguyên 3 nằm trong một tập hợp:

`` `C ++
if (my_set.find (3)! = nullptr) {
// Số nguyên 3 nằm trong tập hợp.
} khác {
// Số nguyên 3 không có trong tập hợp.
}
`` `

## Sắp xếp một bộ

Bạn có thể sắp xếp các phần tử trong một tập hợp bằng phương thức `sort ()`.Phương thức `sort ()` sắp xếp các phần tử trong tập hợp theo thứ tự tăng dần.Ví dụ: mã sau đây sắp xếp các phần tử trong một tập hợp:

`` `C ++
my_set.sort ();
`` `

## băm một bộ

Bạn có thể băm các phần tử trong một tập hợp bằng phương thức `Hash ()`.Phương thức `Hash ()` Trả về giá trị băm cho mỗi phần tử trong tập hợp.Các giá trị băm được sử dụng để lưu trữ các phần tử trong một tập hợp trong bảng băm.Ví dụ: mã sau băm các phần tử trong một tập hợp:

`` `C ++
STD :: Vector <Int> Hash_Values;
for (int phần tử: my_set) {
Hash_values.push_back (my_set.hash (phần tử));
}
`` `

## Bộ băm

Một bộ băm là một loại bộ sử dụng hàm băm để lưu trữ các phần tử trong bảng băm.Các bộ hàm băm hiệu quả hơn các bộ thông thường để lưu trữ các bộ phần tử lớn.

Để tạo
=======================================
#C++, #Set, #C++set, #STL, #Container **Set in C++**

A set is a data structure that stores unique elements in no particular order. It is a collection that is **unordered** and **unindexed**. The elements in a set are **not allowed to be duplicated**.

Sets are implemented using **red-black trees** in the C++ Standard Template Library (STL). Red-black trees are a type of self-balancing binary search tree that guarantees that the following properties are always true:

* The tree is **acyclic**, meaning that there are no cycles in the tree.
* The tree is **balanced**, meaning that the difference in the heights of the left and right subtrees of any node is at most one.
* The tree is **ordered**, meaning that the elements in the tree are stored in ascending order.

Sets are used to store a collection of unique elements. They are often used to check for the existence of an element in a collection, to find the union or intersection of two sets, or to find the elements that are common to two sets.

## Creating a Set

To create a set, you can use the `std::set` class template. The `std::set` class template takes a type parameter that specifies the type of elements that the set will store. For example, the following code creates a set that stores integers:

```c++
std::set<int> my_set;
```

## Adding Elements to a Set

You can add elements to a set using the `insert()` method. The `insert()` method takes an element as its argument and inserts the element into the set. If the element is already in the set, the `insert()` method does nothing. For example, the following code adds the integers 1, 2, and 3 to a set:

```c++
my_set.insert(1);
my_set.insert(2);
my_set.insert(3);
```

## Removing Elements from a Set

You can remove elements from a set using the `erase()` method. The `erase()` method takes an element as its argument and removes the element from the set. If the element is not in the set, the `erase()` method does nothing. For example, the following code removes the integer 2 from a set:

```c++
my_set.erase(2);
```

## Searching for Elements in a Set

You can search for elements in a set using the `find()` method. The `find()` method takes an element as its argument and returns a pointer to the element in the set if it is found. If the element is not found, the `find()` method returns `nullptr`. For example, the following code checks if the integer 3 is in a set:

```c++
if (my_set.find(3) != nullptr) {
// The integer 3 is in the set.
} else {
// The integer 3 is not in the set.
}
```

## Sorting a Set

You can sort the elements in a set using the `sort()` method. The `sort()` method sorts the elements in the set in ascending order. For example, the following code sorts the elements in a set:

```c++
my_set.sort();
```

## Hashing a Set

You can hash the elements in a set using the `hash()` method. The `hash()` method returns a hash value for each element in the set. Hash values are used to store the elements in a set in a hash table. For example, the following code hashes the elements in a set:

```c++
std::vector<int> hash_values;
for (int element : my_set) {
hash_values.push_back(my_set.hash(element));
}
```

## Hash Sets

A hash set is a type of set that uses a hash function to store the elements in a hash table. Hash sets are more efficient than regular sets for storing large sets of elements.

To create
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top