Share c++ hashmap

lacphucnguyen

New member
## C ++ Hashmap

* [Hashmap trong C ++] (https://www.tutorialspoint.com/cplusplus/hashmap_in_cpp.htm)
* [C ++ Unordered_map] (https://www.cplusplus.com/reference/unordered_map/unordered_map/)
* [C ++ Unordered_set] (https://www.cplusplus.com/reference/unordered_set/unordered_set/)
* [C ++ Bảng băm] (Hash table - Wikipedia)
* [C ++ hàm băm] (Hash function - Wikipedia)

# C ++ Hướng dẫn Hashmap

Hashmap là một cấu trúc dữ liệu lưu trữ dữ liệu trong các cặp giá trị khóa.Các khóa được sử dụng để nhanh chóng truy cập các giá trị.Hashmaps thường được sử dụng trong các tình huống mà bạn cần nhanh chóng tra cứu dữ liệu bằng một khóa duy nhất.

Để tạo Hashmap trong C ++, bạn có thể sử dụng lớp `unetered_map`.Lớp `unetered_map` là một lớp được tạo ra, vì vậy bạn cần chỉ định các loại dữ liệu của các khóa và giá trị khi bạn tạo nó.Ví dụ: mã sau đây tạo ra một hashmap lưu trữ các chuỗi dưới dạng các khóa và số nguyên làm giá trị:

`` `C ++
std :: unetered_map <std :: chuỗi, int> hashmap;
`` `

Bạn có thể thêm các phần tử vào một hashmap bằng phương thức `chèn ()`.Phương thức `chèn ()` có hai đối số: khóa và giá trị.Ví dụ: mã sau đây thêm hai phần tử vào hashmap:

`` `C ++
Hashmap.insert ({"key1", 1});
Hashmap.insert ({"key2", 2});
`` `

Bạn có thể truy cập các phần tử trong một hashmap bằng phương thức `at ()`.Phương thức `at ()` lấy phím làm đối số và trả về giá trị được liên kết với khóa đó.Ví dụ: mã sau in giá trị được liên kết với khóa "key1":

`` `C ++
std :: cout << hashmap.at ("key1") << std :: endl;
`` `

Bạn cũng có thể lặp lại các phần tử trong một hashmap bằng các phương thức `started ()` và `end ()`.Phương thức `started ()` trả về một trình lặp lại cho phần tử đầu tiên trong băm và phương thức `end ()` trả về một trình lặp lại cho phần tử sau phần tử cuối cùng trong hashmap.Ví dụ: mã sau in tất cả các yếu tố trong Hashmap:

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

Hashmap là một cấu trúc dữ liệu mạnh mẽ có thể được sử dụng để nhanh chóng lưu trữ và truy cập dữ liệu.Chúng đặc biệt hữu ích trong các tình huống mà bạn cần nhanh chóng tra cứu dữ liệu bằng một khóa duy nhất.
=======================================
## C++ Hashmap

* [Hashmap in C++](https://www.tutorialspoint.com/cplusplus/hashmap_in_cpp.htm)
* [C++ unordered_map](https://www.cplusplus.com/reference/unordered_map/unordered_map/)
* [C++ unordered_set](https://www.cplusplus.com/reference/unordered_set/unordered_set/)
* [C++ hash table](https://en.wikipedia.org/wiki/Hash_table)
* [C++ hash function](https://en.wikipedia.org/wiki/Hash_function)

# C++ Hashmap Tutorial

A hashmap is a data structure that stores data in key-value pairs. The keys are used to quickly access the values. Hashmaps are often used in situations where you need to quickly look up data by a unique key.

To create a hashmap in C++, you can use the `unordered_map` class. The `unordered_map` class is a templated class, so you need to specify the data types of the keys and values when you create it. For example, the following code creates a hashmap that stores strings as keys and integers as values:

```c++
std::unordered_map<std::string, int> hashmap;
```

You can add elements to a hashmap using the `insert()` method. The `insert()` method takes two arguments: the key and the value. For example, the following code adds two elements to the hashmap:

```c++
hashmap.insert({"key1", 1});
hashmap.insert({"key2", 2});
```

You can access elements in a hashmap using the `at()` method. The `at()` method takes the key as an argument and returns the value associated with that key. For example, the following code prints the value associated with the key "key1":

```c++
std::cout << hashmap.at("key1") << std::endl;
```

You can also iterate over the elements in a hashmap using the `begin()` and `end()` methods. The `begin()` method returns an iterator to the first element in the hashmap, and the `end()` method returns an iterator to the element after the last element in the hashmap. For example, the following code prints all of the elements in the hashmap:

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

Hashmaps are a powerful data structure that can be used to quickly store and access data. They are particularly useful in situations where you need to quickly look up data by a unique key.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top