Share c++ unordered map

duongtoan.thang

New member
## C ++ unetered_map

* [Bảng băm] (Hash table - Wikipedia)
* [Mảng liên kết chưa được đặt hàng] (https://en.wikipedia.org/wiki/unordered_associative_array)
* [C ++ STL] (Standard library header <unordered_map> (C++11) - cppreference.com)

## C ++ unetered_map là gì?

C ++ Unordered_map là một mảng kết hợp lưu trữ các phần tử trong bảng băm.Đây là cấu trúc dữ liệu cho phép bạn lưu trữ dữ liệu ở định dạng giá trị khóa, trong đó khóa được sử dụng để tra cứu giá trị.Unordered_map tương tự như `std :: map`, nhưng nó không đảm bảo thứ tự của các yếu tố.Điều này có thể hữu ích nếu bạn không cần truy cập các yếu tố theo một thứ tự cụ thể.

## Làm thế nào để sử dụng C ++ Unordered_map?

Để sử dụng C ++ Unordered_map, trước tiên bạn cần bao gồm tệp tiêu đề `<Nonedered_map>`.Sau đó, bạn có thể tạo một đối tượng unetre_map bằng cách sử dụng cú pháp sau:

`` `C ++
std :: unetered_map <key, value> map;
`` `

Trong đó `key` là loại của khóa và` value` là loại giá trị.

Sau đó, bạn có thể thêm các phần tử vào unetered_map bằng cách sử dụng cú pháp sau:

`` `C ++
Bản đồ [khóa] = giá trị;
`` `

Trong đó `key` là khóa của phần tử và` value` là giá trị của phần tử.

Bạn có thể truy cập các thành phần trong unetered_map bằng cách sử dụng cú pháp sau:

`` `C ++
value = map [khóa];
`` `

Trong đó `key` là khóa của phần tử bạn muốn truy cập.

Bạn cũng có thể lặp lại các phần tử trong uned_map bằng cách sử dụng cú pháp sau:

`` `C ++
for (auto it = map.begin (); it! = map.end (); ++ it) {
// làm điều gì đó với nó-> đầu tiên và nó-> thứ hai
}
`` `

## Ưu điểm của C ++ UNREDERED_MAP

Có một số lợi thế khi sử dụng C ++ Unordered_map so với các cấu trúc dữ liệu khác.

*** Tra cứu nhanh: ** Unordered_map sử dụng bảng băm để lưu trữ các yếu tố của nó, điều này làm cho nó rất nhanh để tra cứu một phần tử bằng khóa của nó.
*** O (1) Chèn và xóa: ** Unordered_map có o (1) thời gian chèn và xóa, điều đó có nghĩa là nó có thể chèn hoặc xóa một phần tử trong thời gian không đổi.
*** Linh hoạt: ** Unordered_map có thể lưu trữ bất kỳ loại khóa và giá trị nào, miễn là cả hai đều có thể gán cho một `const void*`.

## Nhược điểm của C ++ Unorered_map

Có một vài nhược điểm khi sử dụng C ++ Unordered_map.

*** Không được đặt hàng: ** Unordered_map không đảm bảo thứ tự của các phần tử, điều này có thể gây khó khăn cho việc lặp lại các phần tử theo một thứ tự cụ thể.
*** kém hiệu quả hơn các cấu trúc dữ liệu khác: ** Unordered_map có thể kém hiệu quả hơn các cấu trúc dữ liệu khác, chẳng hạn như `std :: map`, đối với một số hoạt động, chẳng hạn như tìm phần tử đầu tiên hoặc phần cuối trong bản đồ.

## Phần kết luận

C ++ Unordered_map là một cấu trúc dữ liệu mạnh mẽ có thể được sử dụng để lưu trữ dữ liệu ở định dạng giá trị khóa.Nó nhanh, linh hoạt và hiệu quả, làm cho nó trở thành một lựa chọn tốt cho nhiều ứng dụng.

## hashtags

* #C ++
* #cấu trúc dữ liệu
* #Bảng băm
* #Kết nối liên kết
* #bản đồ chưa được đặt hàng
=======================================
## C++ Unordered_Map

* [Hash table](https://en.wikipedia.org/wiki/Hash_table)
* [Unordered associative array](https://en.wikipedia.org/wiki/Unordered_associative_array)
* [C++ STL](https://en.cppreference.com/w/cpp/header/unordered_map)

## What is C++ Unordered_Map?

C++ Unordered_Map is an associative array that stores elements in a hash table. It is a data structure that allows you to store data in a key-value format, where the key is used to look up the value. Unordered_Map is similar to `std::map`, but it does not guarantee the order of the elements. This can be useful if you do not need to access the elements in a specific order.

## How to use C++ Unordered_Map?

To use C++ Unordered_Map, you first need to include the header file `<unordered_map>`. Then, you can create an Unordered_Map object by using the following syntax:

```c++
std::unordered_map<Key, Value> map;
```

where `Key` is the type of the key and `Value` is the type of the value.

You can then add elements to the Unordered_Map by using the following syntax:

```c++
map[key] = value;
```

where `key` is the key of the element and `value` is the value of the element.

You can access elements in the Unordered_Map by using the following syntax:

```c++
value = map[key];
```

where `key` is the key of the element you want to access.

You can also iterate over the elements in the Unordered_Map by using the following syntax:

```c++
for (auto it = map.begin(); it != map.end(); ++it) {
// Do something with it->first and it->second
}
```

## Advantages of C++ Unordered_Map

There are several advantages to using C++ Unordered_Map over other data structures.

* **Fast lookup:** Unordered_Map uses a hash table to store its elements, which makes it very fast to look up an element by its key.
* **O(1) insertion and deletion:** Unordered_Map has O(1) insertion and deletion time, which means that it can insert or delete an element in constant time.
* **Flexible:** Unordered_Map can store any type of key and value, as long as they are both assignable to a `const void*`.

## Disadvantages of C++ Unordered_Map

There are a few disadvantages to using C++ Unordered_Map.

* **Not ordered:** Unordered_Map does not guarantee the order of the elements, which can make it difficult to iterate over the elements in a specific order.
* **Less efficient than other data structures:** Unordered_Map can be less efficient than other data structures, such as `std::map`, for some operations, such as finding the first or last element in the map.

## Conclusion

C++ Unordered_Map is a powerful data structure that can be used to store data in a key-value format. It is fast, flexible, and efficient, making it a good choice for a variety of applications.

## Hashtags

* #C++
* #data-structures
* #hash-tables
* #Associative-arrays
* #unordered-maps
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top