Article posted: http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html
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 of Map
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. The quick Delete record modifies the value record according to 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 has no 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, "a"))
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 up, where you need to refer to the Begin () and end () two members, representing the first and last entry in the Map object, the type of the 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 iterator erase (iterator first, iterator last) through an entry object; Delete a range Size_type erase (const key& Key); Delete Clear () by keyword is equivalent to enummap.erase (Enummap.begin (), Enummap.end ());
7. The basic operation function of map: c++ maps is an associative container that contains "keyword/value" pairs begin () returns an iterator to the map header Clear () Delete all elements count () returns the number of occurrences of the specified element empty () returns True end () If map is empty Returns an iterator pointing to the end of Map equal_range () Returns an iterator for a special entry to erase () Delete an element find () Find an element Get_allocator () Return to map configurator Insert () Insert Elements key_comp () returns the function of the comparison element key Lower_bound () returns the key value >= The first position of a given element max_size () returns the maximum number of elements that can be accommodated rbegin () Returns a reverse iterator that points to the tail of the map rend () returns a reverse iterator pointing to the map header size () returns the number of elements in a map swap () swap two map Upper_bound () return key value > First position of a given element Value_comp () Returns a function that compares the element value
Example:
Traverse:
Map<string,cagent>::iterator ITER; for (iter = M_agentclients.begin (); ITER! = M_agentclients.end (); ++iter) {if (iter->first== "8001" {this-> Sendmsg (iter->second.psocket,strmsg);//iter->first}}
Find:
Map<string,cagent>::iterator Iter=m_agentclients.find (stragentname); if (Iter!=m_agentclients.end ())//has duplicate name {} else//No {}
Number of elements
if (M_agentclients.size () ==0)
Delete
Map<string,cagent>::iterator Iter=m_agentclients.find (Psocket->getname ()); if (Iter!=m_agentclients.end ()) {
M_agentclients.erase (ITER);//List removal}
Basic operation and use of C + + map