Share c++ map,

ngonhaquoc.my

New member
#C ++, #Map, #STL, #C ++ Hướng dẫn, #Datstrate ** Bản đồ C ++: Hướng dẫn đơn giả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 đồ thường được sử dụng để lưu trữ dữ liệu liên kết, chẳng hạn như danh sách tên và số điện thoại.

Trong C ++, các bản đồ được triển khai bằng lớp `std :: map`.Lớp `STD :: MAP` có một số phương thức để thêm, xóa và tìm các yếu tố trong bản đồ.

Để tạo bản đồ, bạn có thể sử dụng hàm tạo `std :: 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ể thêm 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 thêm một phần tử mới vào bản đồ:

`` `C ++
my_map.insert ({"John", 123});
`` `

Bạn có thể tìm thấy các phần tử trong bản đồ bằng phương thức `find ()`.Phương thức `find ()` lấy một khóa làm đối số và trả lại một con trỏ về giá trị được liên kết với khóa đó.Nếu khóa không tồn tại trong bản đồ, phương thức `find ()` trả về `nullptr`.Ví dụ: mã sau tìm thấy giá trị được liên kết với khóa "John":

`` `C ++
int value = my_map.find ("john")-> thứ hai;
`` `

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 trong bản đồ.Ví dụ: mã sau lặp lại trên các phần tử trong bản đồ và in các giá trị của chúng:

`` `C ++
for (auto it = my_map.begin (); nó! = my_map.end (); ++ it) {
std :: cout << 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ữ dữ liệu liên kết.Chúng rất dễ sử dụng và hiệu quả để truy cập.

## 5 hashtags

* #C ++
* #bản đồ
* #STL
* #C ++ Hướng dẫn
* #cấu trúc dữ liệu
=======================================
#C++, #Map, #STL, #C++Tutorial, #datastructure **C++ map: A simple guide**

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 often used to store associative data, such as a list of names and phone numbers.

In C++, maps are implemented using the `std::map` class. The `std::map` class has a number of methods for adding, removing, and finding elements in a map.

To create a map, you can use the `std::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 add elements to a map using the `insert()` method. The `insert()` method takes two arguments: the key and the value. For example, the following code adds a new element to the map:

```c++
my_map.insert({"John", 123});
```

You can find elements in a map using the `find()` method. The `find()` method takes a key as an argument and returns a pointer to the value associated with that key. If the key does not exist in the map, the `find()` method returns `nullptr`. For example, the following code finds the value associated with the key "John":

```c++
int value = my_map.find("John")->second;
```

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 in the map. For example, the following code iterates over the elements in the map and prints their values:

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

Maps are a powerful data structure that can be used to store associative data. They are easy to use and efficient to access.

## 5 Hashtags

* #C++
* #Map
* #STL
* #C++Tutorial
* #datastructure
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top