1. Directory
- Introduction to map
- Map Functions
- Use map
- Insert element in map
- Search for and obtain elements in map
- Delete element from map
2. Introduction to map
Map is an associated container. It features that adding or deleting nodes has little impact on the iterator, except that the Operation node has no impact on other nodes. For the iterator,
You can modify the real value instead of the key.
3. Map Functions
- Automatically create the corresponding Key-value. Key and value can be any type you need.
- You can quickly search for records based on the key value. The search complexity is basically Log (N). If there are 1000 records, you can search for up to 10 records, 1,000,000 records, and up to 20 records.
- Insert Key-Value records quickly.
- Quickly delete records
- Modify the value record based on the Key.
- Traverse all records.
4. Use map
Use map to obtain the header file containing the map class
# Include <map> // note that the STL header file does not have the extension. h.
The map object is a template class and requires two template parameters: keyword and storage object:
Std: map <int, string> personnel;
In this way, we define an index with an int and have the associated pointer to the string.
For ease of use, you can define the type of the template class,
Typedef map <int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
5. Insert element in map
It is very easy to change the entries in the map, because the map class has already overloaded the [] operator.
EnumMap [1] = "One ";
EnumMap [2] = "Two ";
.....
This is very intuitive, but there is a performance problem. When inserting 2, first find the item with the primary key of 2 in enumMap, and then insert a new object into enumMap. The key is 2,
The value is an empty string. After being inserted, the string is assigned as "Two". This method assigns each value to the default value and then to the displayed value, if the element is a class object,
The overhead is relatively large. We can avoid overhead using the following methods:
EnumMap. insert (map <int, CString >:: value_type (2, "Two "))
6. Search for and obtain elements in map
The subscript operator provides the simplest way to obtain a value:
CString tmp = enumMap [2];
However, this parameter is only applicable to instances with this key in map. Otherwise, an instance is automatically inserted and its value is the initialization value.
We can use the Find () and Count () methods to identify whether a key exists.
The find () method is used to check whether a map contains a keyword entry. The input parameter is the key to be searched. here we need to mention two members: begin () and end,
The first and last entries in the map object are represented respectively. The two data types are iterator.
Int nFindKey = 2; // Key to be searched
// Define an entry variable (actually a pointer)
UDT_MAP_INT_CSTRING: iterator it = enumMap. find (nFindKey );
If (it = enumMap. end ()){
// Not found
}
Else {
// Find
}
The iterator data type obtained through the map object method is a std: pair object, which contains two data iterator-> first and iterator-> second representing the keywords and stored data respectively.
7. Delete element from map
Remove an entry from a map using erase ()
The member method is defined as follows:
- Iterator erase (iterator it); // delete an entry object
- Iterator erase (iterator first, iterator last); // delete a range
- Size_type erase (const Key & key); // Delete by using the keyword
Clear () is equivalent to enumMap. erase (enumMap. begin (), enumMap. end ());
1. Directory
- Introduction to map
- Map Functions
- Use map
- Insert element in map
- Search for and obtain elements in map
- Delete element from map
2. Introduction to map
Map is an associated container. It features that adding or deleting nodes has little impact on the iterator, except that the Operation node has no impact on other nodes. For the iterator,
You can modify the real value instead of the key.
3. Map Functions
- Automatically create the corresponding Key-value. Key and value can be any type you need.
- You can quickly search for records based on the key value. The search complexity is basically Log (N). If there are 1000 records, you can search for up to 10 records, 1,000,000 records, and up to 20 records.
- Insert Key-Value records quickly.
- Quickly delete records
- Modify the value record based on the Key.
- Traverse all records.
4. Use map
Use map to obtain the header file containing the map class
# Include <map> // note that the STL header file does not have the extension. h.
The map object is a template class and requires two template parameters: keyword and storage object:
Std: map <int, string> personnel;
In this way, we define an index with an int and have the associated pointer to the string.
For ease of use, you can define the type of the template class,
Typedef map <int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
5. Insert element in map
It is very easy to change the entries in the map, because the map class has already overloaded the [] operator.
EnumMap [1] = "One ";
EnumMap [2] = "Two ";
.....
This is very intuitive, but there is a performance problem. When inserting 2, first find the item with the primary key of 2 in enumMap, and then insert a new object into enumMap. The key is 2,
The value is an empty string. After being inserted, the string is assigned as "Two". This method assigns each value to the default value and then to the displayed value, if the element is a class object,
The overhead is relatively large. We can avoid overhead using the following methods:
EnumMap. insert (map <int, CString >:: value_type (2, "Two "))
6. Search for and obtain elements in map
The subscript operator provides the simplest way to obtain a value:
CString tmp = enumMap [2];
However, this parameter is only applicable to instances with this key in map. Otherwise, an instance is automatically inserted and its value is the initialization value.
We can use the Find () and Count () methods to identify whether a key exists.
The find () method is used to check whether a map contains a keyword entry. The input parameter is the key to be searched. here we need to mention two members: begin () and end,
The first and last entries in the map object are represented respectively. The two data types are iterator.
Int nFindKey = 2; // Key to be searched
// Define an entry variable (actually a pointer)
UDT_MAP_INT_CSTRING: iterator it = enumMap. find (nFindKey );
If (it = enumMap. end ()){
// Not found
}
Else {
// Find
}
The iterator data type obtained through the map object method is a std: pair object, which contains two data iterator-> first and iterator-> second representing the keywords and stored data respectively.
7. Delete element from map
Remove an entry from a map using erase ()
The member method is defined as follows:
- Iterator erase (iterator it); // delete an entry object
- Iterator erase (iterator first, iterator last); // delete a range
- Size_type erase (const Key & key); // Delete by using the keyword
Clear () is equivalent to enumMap. erase (enumMap. begin (), enumMap. end ());