1.Map Introduction
map is a type of associative container. It is characterized by the small effect of adding and removing nodes on iterators, except for the Operation node, which has no effect on other nodes. For iterators, you can modify the real value without modifying the key.
2. Function ofmap
Automatically establishes the correspondence of the Key-value. The key and value can be any type you want.
Quickly find records based on key values, finding the complexity is basically log (N), if there are 1000 records, find up to 10 times, 1,000,000 records, up to 20 times to find.
Quickly insert Key-value Records.
Quickly delete records
Modify the value record by key.
Traverse all records.
3. Use map
Use map to include the header file where the map class resides
#include <map>//Note that the STL header file does not have an extension. h
The map object is a template class that requires two template parameters for the keyword and storage object:
std:map<int, string> personnel;
This defines an int as an index and a pointer to a string that is associated with it.
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;
4. Inserting elements into a map
Changing the entries in the map is simple because the map class has overloaded the [] operator
ENUMMAP[1] = "one";
ENUMMAP[2] = "both";
.....
This is very intuitive, but there is a performance issue. Insert 2 o'clock, first find the key 2 in Enummap, not found, and then insert a new object Enummap, the key is 2, the value is an empty string, after the insertion is completed, the string is assigned "two"; This method assigns each value to the default value, which is then assigned to the displayed value, which is more expensive if the element is a class object. We can avoid the overhead in the following ways:
Enummap.insert (map<int, cstring>:: Value_type (2, "I"))
5. Find and get the elements in the map
The subscript operator gives the simplest way to get a value:
CString tmp = enummap[2];
However, it is only true if there is an instance of the key in the map , or an instance is automatically inserted, and the value is the initialization value.
We can use the find () and count () methods to discover whether a key exists.
Find out if the map contains a keyword entry with the Find () method, the parameter passed in is the key to look for, where you need to mention the Begin () and end () two members, representing the first and last entries in the map object, respectively. The type of these two data is iterator.
int nfindkey = 2; Key to look for
Define an entry variable (actually a pointer)
Udt_MAP_int_cstring::iterator it= enummap.find (Nfindkey);
if (it = = Enummap.end ()) {
Didn't find
}
else {
Found it
}
The iterator data type obtained by the method of the map object is an std::p air object, including two data Iterator->first and iterator->second representing the keywords and stored data respectively
6. Delete elements from the map
Remove an entry from a map with erase ()
The member method is defined as follows
Iterator Erase (iterator it); Delete by an entry object
Iterator Erase (iterator first, iterator last); Delete a range
Size_type Erase (const key& Key); Delete by keyword
Clear () is equivalent to Enummap.erase (Enummap.begin (), Enummap.end ());
C + + use of STLmaps
The following are examples of inserting, locating, traversing, and deleting STLmaps in C + + :
#include <map>
#include <string>
#include <iostream>
using namespace Std;
void map_insert (map < string, string > *mapstudent, String Index, String x)
{
Mapstudent->insert (map < String, string >::value_type (index, x));
}
int main (int argc, char **argv)
{
Char tmp[32] = "";
Map < string, string > MapS;
Insert Element
map_insert (&maps, "192.168.0.128", "Xiong");
map_insert (&maps, "192.168.200.3", "Feng");
map_insert (&maps, "192.168.200.33", "Xiongfeng");
Map < String, string >::iterator iter;
cout << "We have third Element:" << Endl;
cout << "-----------------------------" << Endl;
Find Element
iter = Maps.find ("192.168.0.33");
if (iter! = Maps.end ()) {
cout << "Find the Elememt" << Endl;
cout << "It is:" << iter->second << Endl;
} else {
cout << "not find the element" << Endl;
}
See element
for (iter = Maps.begin (); ITER! = Maps.end (); iter) {
cout << "|" << iter->first << "|" << iter->
Second << "|" << Endl;
}
cout << "-----------------------------" << Endl;
map_insert (&maps, "192.168.30.23", "XF");
cout << "After We Insert one Element:" << Endl;
cout << "-----------------------------" << Endl;
for (iter = Maps.begin (); ITER! = Maps.end (); iter) {
cout << "|" << iter->first << "|" << iter->
Second << "|" << Endl;
}
cout << "-----------------------------" << Endl;
Delete Element
iter = Maps.find ("192.168.200.33");
if (iter! = Maps.end ()) {
cout << "Find the element:" << iter->first << Endl;
cout << "Delete element:" << iter->first << Endl;
cout << "=================================" << Endl;
Maps.erase (ITER);
} else {
cout << "not find the element" << Endl;
}
for (iter = Maps.begin (); ITER! = Maps.end (); iter) {
cout << "|" << iter->first << "|" << iter->
Second << "|" << Endl;
}
cout << "=================================" << Endl;
return 0;
}
Basic operation and use of C++map