Share c++ 98 map

## C ++ 98, MAP, STL, Mảng liên kết

** C ++ 98 Bản đồ: Hướng dẫn **

Bản đồ là một cấu trúc dữ liệu lưu trữ các cặp giá trị khóa.Các khóa là duy nhất và các giá trị có thể thuộc bất kỳ loại nào.Bản đồ được triển khai bằng lớp mảng kết hợp STL.

Để tạo bản đồ, bạn có thể sử dụng hàm tạo `map ()`.Hàm tạo có hai đối số: loại khóa và loại giá trị.Ví dụ: mã sau đây tạo ra một bản đồ chuỗi cho các số nguyên:

`` `C ++
std :: map <std :: chuỗi, int> my_map;
`` `

Bạn có thể chèn các phần tử vào bản đồ 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 chèn cặp giá trị khóa "FOO" vào 10 vào bản đồ:

`` `C ++
my_map.insert ({"foo", 10});
`` `

Bạn có thể truy cập các phần tử trong bản đồ 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ị tương ứng.Ví dụ: mã sau in giá trị của khóa "foo":

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

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

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

Bản đồ là một cấu trúc dữ liệu mạnh mẽ có thể được sử dụng để lưu trữ và sắp xếp dữ liệu.Chúng đặc biệt hữu ích khi bạn cần nhanh chóng tìm thấy một yếu tố dựa trên khóa của nó.

** Hashtags: **

* C ++
* STL
* Mảng kết hợp
* Cấu trúc dữ liệu
* Hướng dẫn
=======================================
## C++ 98, map, STL, associative array

**C++ 98 map: A tutorial**

A map is a data structure that stores key-value pairs. The keys are unique, and the values can be of any type. Maps are implemented using the STL associative array class.

To create a map, you can use the `map()` constructor. The constructor takes two arguments: the type of the keys and the type of the values. For example, the following code creates a map of strings to integers:

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

You can insert elements into a map using the `insert()` method. The `insert()` method takes two arguments: the key and the value. For example, the following code inserts the key-value pair "foo" to 10 into the map:

```c++
my_map.insert({"foo", 10});
```

You can access elements in a map 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 key "foo":

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

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

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

Maps are a powerful data structure that can be used to store and organize data. They are particularly useful when you need to quickly find an element based on its key.

**Hashtags:**

* C++
* STL
* Associative array
* Data structure
* Tutorial
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top