Reprinted from: http://blog.csdn.net/sunquana/article/details/12576729
First, the definition
(1) map<string, int> map;
(2) or: typedef map<string,int> MYMAP;
Mymap Map;
Second, insert data
Before inserting the data, let's talk about the use of pair and Make_pair.
Pair is a struct with first and second two domains that can be accessed directly
1 string key="Sunquan"; 2 int value=123456; 3 Pair <string,int> b (Key, value); Here The pair <string,string> is the data type, followed by the modulation parameter construction method 4
While Make_pair is returning a pair < type, type > Data, Eg:make_pair ("Asa", 123456); But you have to find a pair <string,int> type of variable to accept the return value.
Next step:
(1) map["abc"]=1;
(2) Map.insert (pair<string,int> ("C", 3));
(3) Map.insert (make_pair<string,int> ("D", 4));
Third, modify and find data
(1) Modify the map["Sunquan"]=11111;
(2) Find data with Map.find (key); Can be checked by key.
Remember not to use int value=map[key]; This will add the key to the MAP, and value is the default (int is 0,string is an empty string).
By means of method (2), the address of the iterator is returned, the value of the iterator is Map.end () and the key does not exist;
Iv. deleting elements
(1) Delete by key;
(2) by the iterator to delete;
Here's a look at the detailed code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <map>6 using namespacestd; 7 8 intMain ()9 { Tenmap<string,int>Map; Onemap<string,int>:: Iterator it; AMap.insert (pair<string,int> ("Root", A)); -Map.insert (pair<string,int> ("Scot", One)); - for(It=map.begin (); It!=map.end (); it++) thecout<<it->first<<" "<<it->second<<Endl; -it=Map.begin (); -Map.erase (IT);//Remove by iterator - stringkey="Root"; +Map.erase (key);//Delete via key - +Map.erase (Map.begin (), Map.end ());//one iterator to another iterator A //equivalent to Map.clear (); at - for(It=map.begin (); It!=map.end (); it++) -cout<<it->first<<" "<<it->second<<Endl; - return 0; -}
Note:
Map<int, String>::iterator It is declaring an iterator
Map<int, string> It is declaring a map container
V. Some methods of map in C + +
Begin () returns an iterator pointing to the map header
Clear () Delete all elements
COUNT () returns the number of occurrences of the specified element
Empty () returns True if Map is empty
End () returns an iterator pointing to the end of the map
Equal_range () returns an iterator to a special entry
Erase () Delete an element
Find () finds an element
Insert () Inserts an element
Max_size () returns the maximum number of elements that can be accommodated
Size () returns the number of elements in the map
Swap () Swap two maps
Get_allocator () returns the map's Configurator
Key_comp () returns the function that compares the 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 pointing to the tail of the map
Rend () returns a reverse iterator pointing to the map header
Upper_bound () returns the key value > the first position of a given element
Value_comp () returns a function that compares the element value
C language · A detailed description of map usage in C + +