1. Map Introduction
Map is a type of associative container. It is characterized by the addition and deletion of the node to the iterator has less impact, in addition to the Operation node, the other nodes have no effect. 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.
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 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" ;.....
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" ))
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 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 ()) {// not found } Else {// found }
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
//Size_type Erase (const// deleted via keyword)
Clear () is equivalent to Enummap.erase (Enummap.begin (), Enummap.end ());
7, the basic operation of map function:
C + + map is an associative container that contains "keyword/value" pairs, which are commonly used for the following functions:
Begin ()//returns an iterator pointing to the map headerClear ()//Delete all elementsCount ()//returns the number of occurrences of the specified elementEmpty ()//returns True if Map is emptyEnd ()//returns an iterator pointing to the end of the mapEqual_range ()//returns an iterator to a special entryErase ()//Delete an elementFind ()//Find an elementGet_allocator ()//return to map configuratorInsert ()//inserting elementsKey_comp ()//returns a function that compares the element keyLower_bound ()//returns the key value >= the first position of a given elementMax_size ()//returns the maximum number of elements that can be accommodatedRbegin ()//returns a reverse iterator that points to the tail of the mapRend ()//returns a reverse iterator pointing to the map headerSize ()//returns the number of elements in a mapSwap ()//Exchange Two mapUpper_bound ()//returns the key value > the first position of a given elementValue_comp ()//returns a function that compares the element value
Example:
//Traversal: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); } }//Find:map<string, Cagent>::iterator iter=M_agentclients.find (stragentname);if(Iter!=m_agentclients.end ())//have found { } Else//No { }//Number of elementsif(m_agentclients.size () = =0)//Deletemap<string, Cagent>::iterator Iter=m_agentclients.find (psocket->GetName ()); if(iter!=M_agentclients.end ()) {m_agentclients.erase (ITER);//List Removal}
Transferred from: http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html
Basic operation and use of "go" C + + map