Share unordered_map c++

happyfish646

New member
#Unordered_map #C ++ #hashtable #associative_array #Map ## unetered_map trong c ++

Unetered_map là một cấu trúc dữ liệu lưu trữ các cặp giá trị khóa trong bảng băm.Nó tương tự như bản đồ, nhưng nó không đảm bảo rằng các yếu tố sẽ được lưu trữ theo bất kỳ thứ tự cụ thể nào.Điều này có thể hữu ích cho các ứng dụng cần nhanh chóng truy cập dữ liệu mà không phải lo lắng về thứ tự được lưu trữ.

Để tạo một unetered_map, bạn có thể sử dụng cú pháp sau:

`` `C ++
Unordered_map <key_type, value_type> my_map;
`` `

Trong đó `key_type` là loại của các khóa trong bản đồ và` value_type` là loại của các giá trị.

Bạn có thể chèn các phần tử vào một phương thức unetered_map bằng phương thức `chèn ()`.Ví dụ:

`` `C ++
my_map.insert ({"key1", "value1"});
my_map.insert ({"key2", "value2"});
`` `

Bạn có thể truy cập các thành phần trong một uned_map bằng phương thức `at ()`.Ví dụ:

`` `C ++
std :: cout << my_map.at ("key1") << std :: endl;// in "value1"
`` `

Bạn cũng có thể lặp lại các phần tử trong một phương thức uned_map bằng cách sử dụng các phương thức `started ()` và `end ()`.Ví dụ:

`` `C ++
for (auto it = my_map.begin (); nó! = my_map.end (); ++ it) {
std :: cout << it-> đầu tiên << ":" << it-> thứ hai << std :: endl;
}
`` `

## Hashtables

Một không theo thứ tự_map được triển khai bằng bảng băm.Bảng băm là một cấu trúc dữ liệu lưu trữ dữ liệu trong một bảng xô.Mỗi thùng được liên kết với hàm băm, là một hàm ánh xạ các phím cho số nguyên.Hàm băm được sử dụng để xác định nhóm nào nên được lưu trữ khóa.

Khi một khóa được chèn vào bảng băm, nó được băm trước tiên bằng hàm băm.Số nguyên kết quả sau đó được sử dụng để lập chỉ mục vào bảng băm.Nếu xô ở chỉ mục đó trống, chìa khóa được lưu trữ trong xô.Nếu xô không trống, chìa khóa trước tiên được so sánh với các khóa của các yếu tố khác trong xô.Nếu khóa đã có trong xô, giá trị mới ghi đè lên giá trị cũ.Nếu khóa không có trong xô, cặp giá trị khóa mới được thêm vào xô.

## Mảng liên kết

Một uneter_map là một loại mảng kết hợp.Một mảng kết hợp là một cấu trúc dữ liệu lưu trữ dữ liệu ở định dạng giá trị khóa.Các khóa được sử dụng để truy cập các giá trị.Các mảng liên kết thường được sử dụng để lưu trữ dữ liệu không theo thứ tự cụ thể.

## Bản đồ

Bản đồ là một loại mảng kết hợp đảm bảo rằng các yếu tố sẽ được lưu trữ theo một thứ tự cụ thể.Thứ tự này được xác định bởi thứ tự trong đó các yếu tố được chèn vào bản đồ.Bản đồ thường được sử dụng để lưu trữ dữ liệu cần được truy cập theo một thứ tự cụ thể.

## Phần kết luận

Unetered_map là một cấu trúc dữ liệu lưu trữ các cặp giá trị khóa trong bảng băm.Nó tương tự như bản đồ, nhưng nó không đảm bảo rằng các yếu tố sẽ được lưu trữ theo bất kỳ thứ tự cụ thể nào.Điều này có thể hữu ích cho các ứng dụng cần nhanh chóng truy cập dữ liệu mà không phải lo lắng về thứ tự được lưu trữ.
=======================================
#Unordered_map #C++ #hashtable #associative_array #Map ## Unordered_map in C++

An unordered_map is a data structure that stores key-value pairs in a hash table. It is similar to a map, but it does not guarantee that the elements will be stored in any particular order. This can be useful for applications that need to quickly access data without having to worry about the order in which it is stored.

To create an unordered_map, you can use the following syntax:

```c++
unordered_map<key_type, value_type> my_map;
```

where `key_type` is the type of the keys in the map, and `value_type` is the type of the values.

You can insert elements into an unordered_map using the `insert()` method. For example:

```c++
my_map.insert({"key1", "value1"});
my_map.insert({"key2", "value2"});
```

You can access elements in an unordered_map using the `at()` method. For example:

```c++
std::cout << my_map.at("key1") << std::endl; // prints "value1"
```

You can also iterate over the elements in an unordered_map using the `begin()` and `end()` methods. For example:

```c++
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
```

## Hashtables

An unordered_map is implemented using a hash table. A hash table is a data structure that stores data in a table of buckets. Each bucket is associated with a hash function, which is a function that maps keys to integers. The hash function is used to determine which bucket a key should be stored in.

When a key is inserted into a hash table, it is first hashed using the hash function. The resulting integer is then used to index into the hash table. If the bucket at that index is empty, the key is stored in the bucket. If the bucket is not empty, the key is first compared to the keys of the other elements in the bucket. If the key is already present in the bucket, the new value overwrites the old value. If the key is not present in the bucket, the new key-value pair is added to the bucket.

## Associative arrays

An unordered_map is a type of associative array. An associative array is a data structure that stores data in a key-value format. The keys are used to access the values. Associative arrays are often used to store data that is not in a particular order.

## Maps

A map is a type of associative array that guarantees that the elements will be stored in a specific order. This order is determined by the order in which the elements are inserted into the map. Maps are often used to store data that needs to be accessed in a specific order.

## Conclusion

An unordered_map is a data structure that stores key-value pairs in a hash table. It is similar to a map, but it does not guarantee that the elements will be stored in any particular order. This can be useful for applications that need to quickly access data without having to worry about the order in which it is stored.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top