Share c++ unordered_map source

heavyladybug870

New member
: [Liên kết đến bài viết tham khảo của bạn]

#C ++ #Unordered_map #hash Bảng #hash Hàm #data Cấu trúc

## C ++ unetered_map là gì?

C ++ Unordered_map là một cấu trúc dữ liệu thực hiện loại dữ liệu trừu tượng mảng kết hợp.Nó tương tự như STD :: MAP, nhưng nó sử dụng bảng băm để lưu trữ các phần tử, cho phép truy cập nhanh hơn STD :: MAP.

## 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 đề <Nonoreded_map>.Sau đó, bạn có thể tạo một đối tượng unetre_map bằng cách chỉ định loại khóa và loại giá trị.Ví dụ: mã sau đây tạo ra một uneter_map 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> my_map;
`` `

Sau đó, bạn có thể thêm các phần tử vào unetered_map bằng cách sử dụ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 Unorered_map:

`` `C ++
my_map.insert ({"foo", 1});
my_map.insert ({"bar", 2});
`` `

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

`` `C ++
std :: cout << my_map.at ("foo") << std :: endl;
`` `

## Ư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 STD :: MAP.Đầu tiên, Unordered_map nhanh hơn STD :: MAP vì nó sử dụng bảng băm để lưu trữ các yếu tố.Điều này có nghĩa là nó có thể truy cập các phần tử trong thời gian O (1), nhanh hơn nhiều so với thời gian O (log n) theo yêu cầu của STD :: MAP.

Thứ hai, Unordered_map hiệu quả hơn về bộ nhớ hơn STD :: MAP vì nó không cần lưu trữ các phần tử theo thứ tự được sắp xếp.Đây có thể là một lợi thế đáng kể nếu bạn đang làm việc với các bộ dữ liệu lớn.

Cuối cùng, Unordered_map linh hoạt hơn STD :: MAP vì nó cho phép bạn sử dụng bất kỳ loại khóa hoặc giá trị nào.Điều này làm cho nó trở thành một cấu trúc dữ liệu linh hoạt hơn STD :: MAP.

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

Có hai nhược điểm chính đối với việc sử dụng C ++ Unordered_map.Đầu tiên, Unordered_map không đảm bảo rằng các yếu tố sẽ được lưu trữ theo thứ tự được sắp xếp.Đây có thể là một bất lợi nếu bạn cần lặp lại các yếu tố theo thứ tự được sắp xếp.

Thứ hai, Unordered_map không hỗ trợ cùng một phạm vi dựa trên cú pháp Loop như STD :: MAP.Đây có thể là một bất lợi nếu bạn đã quen sử dụng vòng lặp dựa trên phạm vi để lặp lại các phần tử của một container.

## 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ữ và truy cập dữ liệu hiệu quả.Nó nhanh hơn và hiệu quả hơn so với STD :: MAP, nhưng nó không đảm bảo rằng các phần tử sẽ được lưu trữ theo thứ tự được sắp xếp và nó không hỗ trợ cùng một phạm vi dựa trên cú pháp Loop.

## Đọc thêm

* [C ++ Unordered_map Tài liệu] (std::unordered_map - cppreference.com)
* [Hướng dẫn C ++ Unordered_map] (https://www.learncpp.com/cpp-tutorial/unordered-maps/)
* [C ++ unetered_map ví dụ] (https://www.tutorialspoint.com/cplusplus/cpp_unordered_map.htm)
=======================================
: [link to your reference article]

#C++ #Unordered_map #hash Table #hash Function #data Structure

## What is C++ Unordered_Map?

C++ Unordered_Map is a data structure that implements the associative array abstract data type. It is similar to std::map, but it uses a hash table to store the elements, which allows for faster access than std::map.

## 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 specifying the type of the key and the type of the value. For example, the following code creates an unordered_map that stores strings as keys and integers as values:

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

You can then add elements to the unordered_map by 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 unordered_map:

```c++
my_map.insert({"foo", 1});
my_map.insert({"bar", 2});
```

You can access elements in the unordered_map by using the `at()` method. The `at()` method takes the key as an argument and returns the corresponding value. For example, the following code prints the value of the element with the key "foo":

```c++
std::cout << my_map.at("foo") << std::endl;
```

## Advantages of C++ Unordered_Map

There are several advantages to using C++ Unordered_Map over std::map. First, unordered_map is faster than std::map because it uses a hash table to store the elements. This means that it can access elements in O(1) time, which is much faster than the O(log n) time required by std::map.

Second, unordered_map is more memory efficient than std::map because it does not need to store the elements in sorted order. This can be a significant advantage if you are working with large datasets.

Finally, unordered_map is more flexible than std::map because it allows you to use any type of key or value. This makes it a more versatile data structure than std::map.

## Disadvantages of C++ Unordered_Map

There are two main disadvantages to using C++ Unordered_Map. First, unordered_map does not guarantee that the elements will be stored in sorted order. This can be a disadvantage if you need to iterate over the elements in sorted order.

Second, unordered_map does not support the same range-based for loop syntax as std::map. This can be a disadvantage if you are used to using the range-based for loop to iterate over the elements of a container.

## Conclusion

C++ Unordered_Map is a powerful data structure that can be used to store and access data efficiently. It is faster and more memory efficient than std::map, but it does not guarantee that the elements will be stored in sorted order and it does not support the same range-based for loop syntax.

## Further Reading

* [C++ Unordered_Map Documentation](https://en.cppreference.com/w/cpp/container/unordered_map)
* [C++ Unordered_Map Tutorial](https://www.learncpp.com/cpp-tutorial/unordered-maps/)
* [C++ Unordered_Map Examples](https://www.tutorialspoint.com/cplusplus/cpp_unordered_map.htm)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top