Map is a set of key-value pairs. It can be understood as an associated array. You can use a key as a subscript to obtain a value.
URL: http://www.cnblogs.com/archimedes/p/cpp-map.html.
Map object definition
Before using the map header file, you must specify the key and value types respectively:
map<string,int>word_count;
Map constructor:
Map <k, v> m; create an empty map object named m, whose key value types are k and v
Map <k, v> m (m2); Create a copy of m2 m, m and m2 must have the same key value type
Map <k, v> m (B, e); create a map-type object and store copies of all elements within the range marked by iterator B and e, the element type must be converted to pair <const k, v>
Map-defined types
The elements of a map object are key-value pairs. The value_type of map reflects the fact that value_type is the pair type of the key and value of the stored element, and the key is const, for example, the word_count type is:
Pair <const string, int> type
The type defined by the map class:
Map <K, V >:: key_type in the map container, used as the index key type
Map <K, V >:: mapped_type in the map container, the type of the value associated with the key
Map <K, V >:: value_type is a pair type. Its first element has the const map <K, V >:: key_type type, while the second element is map <K, v>: mapped_type
The map iterator will produce pair-type objects for unreferencing.
map<string,int>::iterator map_it=word_count.begin();cout<<map_it->first;cout<<" "<<map_it->second;map_it->first="new key" //error++map_it->second
Add elements to map
Use the insert MEMBER or use the subscript operator to obtain the element, and then assign values to the obtained element.
Use subscript to Access map Objects
Map <string, int> woed_count; // null mapword_count ["Anna"] = 1; // Insert the default initial element (key: Anna, value: 1)
The subscript of map uses indexes (that is, keys) to obtain the values associated with the key. If the key is already in the container, the subscript operation of map is the same, returns the value associated with the key. Only when the queried key does not exist can the map container create a new element for the key and insert it into the map object.
1. Use of subscript operator return values
The subscript operator returns the left value, even if the value associated with a specific key
cout<<word_count["Anna"];++word_count["Anna"];count<<word_count["Anna"];
2. Programming significance of subscript Behavior
If the keys represented by the subscript are not in the container, add new elements. This feature can make the program incredibly concise:
map<string, int>word_count;string word;while(cin>>word) ++word_count[word];
This program is used to record the number of times each word appears
Programming Exercise: compile a program to count and output the number of times the words are read.
#include<iostream>#include<string>#include<vector>#include<map>#include<utility>using namespace std;int main(){ map<string, int> word_count; string word; while(cin>>word) ++word_count[word]; map<string, int>::iterator it; for(it=word_count.begin(); it!=word_count.end(); it++) cout<<it->first<<":"<<it->second<<endl; return 0;}Map: insert usage
Insert a single element using a key-Value pair type parameter. If the parameter is a pair of iterator versions, the iterator must point to the key-Value pair type element.
Returned type of the insert version of the map container that accepts a single value
When a subscript is used to add a new element to the map, the value part of the element will be initialized with the value, and another method for inserting the element is to directly use the insert MEMBER, which makes the syntax more compact:
word_count.insert(map<string,int>::value_type("Anna",1));
The arguments passed to insert are rather clumsy and can be simplified in two ways:
Use make_pair
word_count.insert(make_pair("Anna",1));
Or use typedef:
typedef map<string, int>::value_type valtype;word_count.insert(valtype("Anna", 1))
Checks the return value of insert.
If the key of the element to be inserted is already in the container, insert will not perform any operation, but the insert version with a key-value pair will return a value: contains an iterator and a pair object with a bool value. The iterator points to an element with a corresponding key in the map, and the bool value indicates whether the element is inserted.
The following is a word statistics program rewritten using insert:
#include<iostream>#include<string>#include<vector>#include<map>#include<utility>using namespace std;int main(){ map<string, int> word_count; string word; while(cin>>word) { pair<map<string, int>::iterator, bool> ret=word_count.insert(make_pair(word,1)); if(!ret.second) ++ret.first->second; } map<string, int>::iterator it; for(it=word_count.begin(); it!=word_count.end(); it++) cout<<it->first<<":"<<it->second<<endl; return 0;}Search for and read elements in map
The subscript operator reads a value, causing side effects. The map container provides two operations: count and find, which are used to check whether a key exists without inserting the key.
M. count (k) returns the number of k occurrences in m.
M. find (k) If the m container contains an element indexed by k, the iterator pointing to the element is returned. If not, the system returns the iterator that exceeds the end iterator.
1. Use count to check whether a key in the map object exists
For a map object, the return value of the count member can only be 0 or 1. The map container allows only one key to correspond to one instance. All count can effectively indicate whether a key exists.
map<string, int> word_count;int occurs=0;if(word_count.count("foo")) occurs=word_count["foo"];
2. Read the element without inserting it.
The find operation returns the iterator pointing to the element. If the element does not exist, the end iterator is returned.
map<string, int> word_count;int occurs=0;map<string,int>::iterator it=word_count.find("foo");;if(it!=word_count.end()) occurs=it->second;Delete an element from a map object