Share c++ unordered_set

thuylienpham

New member
#C ++ #Unordered_set #hash_table #data_structure #STL ## C ++ uneded_set

Unset_set của C ++ là một cấu trúc dữ liệu thực hiện ADT đã đặt bằng bảng băm.Nó cung cấp thời gian khấu hao O (1) để chèn, xóa và tra cứu.

## Bảng băm là gì?

Bảng băm là một cấu trúc dữ liệu ánh xạ các phím lên các giá trị.Các phím được băm vào một bảng kích thước cố định và các giá trị được lưu trữ tại các vị trí bảng tương ứng.Các bảng băm có hiệu quả để lưu trữ và truy xuất dữ liệu, vì thời gian tra cứu không phụ thuộc vào số lượng các phần tử trong bảng.

## Làm thế nào để unsored_set hoạt động?

Unordered_set sử dụng bảng băm để lưu trữ các yếu tố của nó.Hàm băm được sử dụng để ánh xạ từng phần tử đến một vị trí bảng duy nhất.Khi một phần tử được chèn vào unetered_set, trước tiên nó được băm vào vị trí bảng.Nếu vị trí bảng trống, phần tử được chèn vào bảng.Nếu vị trí bảng không trống, phần tử được so sánh với phần tử hiện có ở vị trí đó.Nếu các phần tử bằng nhau, phần tử mới không được chèn.Nếu các phần tử không bằng nhau, phần tử mới được chèn sau phần tử hiện có.

## Khi nào tôi nên sử dụng unetered_set?

Nên sử dụng UNREDERED_SET khi bạn cần một tập hợp cung cấp thời gian khấu hao O (1) để chèn, xóa và tra cứu.Unordered_set cũng là một lựa chọn tốt khi bạn cần lặp lại các yếu tố của tập hợp theo thứ tự ngẫu nhiên.

## Ví dụ

Mã sau đây cho thấy cách tạo một phần tử chưa được đặt hàng và chèn vào nó:

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

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

int main () {
// Tạo một số nguyên chưa được đặt hàng.
Unorered_set <Int> s;

// Chèn một số phần tử vào unetered_set.
s.insert (1);
s.insert (2);
s.insert (3);

// lặp lại các yếu tố của uneter_set.
for (int i: s) {
cout << i << endl;
}

trả lại 0;
}
`` `

## Bảng băm

Bảng băm là một cấu trúc dữ liệu ánh xạ các phím lên các giá trị.Các phím được băm vào một bảng kích thước cố định và các giá trị được lưu trữ tại các vị trí bảng tương ứng.Các bảng băm có hiệu quả để lưu trữ và truy xuất dữ liệu, vì thời gian tra cứu không phụ thuộc vào số lượng các phần tử trong bảng.

## Cấu trúc dữ liệu

Cấu trúc dữ liệu là một cách tổ chức dữ liệu để nó có thể được truy cập và thao tác hiệu quả.Cấu trúc dữ liệu được sử dụng trong nhiều ứng dụng khác nhau, bao gồm các chương trình máy tính, cơ sở dữ liệu và hệ điều hành.

## STL

Thư viện mẫu tiêu chuẩn (STL) là một tập hợp các lớp mẫu C ++ cung cấp các cấu trúc và thuật toán dữ liệu chung.STL được thiết kế để di động và hiệu quả, và nó được sử dụng rộng rãi trong lập trình C ++.

## hashtags

* #C ++
* #Unordered_set
* #hash_table
* #Cấu trúc dữ liệu
* #STL
=======================================
#C++ #Unordered_set #hash_table #data_structure #STL ## C++ Unordered_set

C++'s unordered_set is a data structure that implements the set ADT using a hash table. It provides O(1) amortized time for insertion, deletion, and lookup.

## What is a hash table?

A hash table is a data structure that maps keys to values. The keys are hashed to a fixed size table, and the values are stored at the corresponding table positions. Hash tables are efficient for storing and retrieving data, as the lookup time is independent of the number of elements in the table.

## How does unordered_set work?

Unordered_set uses a hash table to store its elements. The hash function is used to map each element to a unique table position. When an element is inserted into the unordered_set, it is first hashed to a table position. If the table position is empty, the element is inserted into the table. If the table position is not empty, the element is compared to the existing element at that position. If the elements are equal, the new element is not inserted. If the elements are not equal, the new element is inserted after the existing element.

## When should I use unordered_set?

Unordered_set should be used when you need a set that provides O(1) amortized time for insertion, deletion, and lookup. Unordered_set is also a good choice when you need to iterate over the elements of the set in a random order.

## Example

The following code shows how to create an unordered_set and insert elements into it:

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

using namespace std;

int main() {
// Create an unordered_set of integers.
unordered_set<int> s;

// Insert some elements into the unordered_set.
s.insert(1);
s.insert(2);
s.insert(3);

// Iterate over the elements of the unordered_set.
for (int i : s) {
cout << i << endl;
}

return 0;
}
```

## Hash table

A hash table is a data structure that maps keys to values. The keys are hashed to a fixed size table, and the values are stored at the corresponding table positions. Hash tables are efficient for storing and retrieving data, as the lookup time is independent of the number of elements in the table.

## Data structure

A data structure is a way of organizing data so that it can be efficiently accessed and manipulated. Data structures are used in a wide variety of applications, including computer programs, databases, and operating systems.

## STL

The Standard Template Library (STL) is a set of C++ template classes that provide common data structures and algorithms. The STL is designed to be portable and efficient, and it is widely used in C++ programming.

## Hashtags

* #C++
* #Unordered_set
* #hash_table
* #data_structure
* #STL
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top