Map and multimap provide methods to operate < key, value > pairs, and store a pair of objects, namely key objects and value objects. Key objects are keys used in the search process, and values are additional data corresponding to keys. For example, if the key is a word, the corresponding value is a number indicating the number of times the word appears in the document. Such a map becomes a frequency table for counting the number of times the word appears in the text; Elements in a map are not allowed to repeat, while elements in a multimap are allowed to repeat.
Although the elements in the set/multiset set only include keys, while the elements in the map map are composed of < key, value > pairs, map / multiset
The provided operation is also performed for the keys in each element, and its operation method is the same as that of the set set operation key. The operation method of set/multiset set described above is also applicable to map/multimap, including the establishment method of set, member function, comparison operation, sorting rules and methods. Only change the set into map and multiset into multimap. Therefore, there are only two supplementary explanations for the insert member function and element access method of map/multimap set:
(1) About insert member function
The elements inserted into map/multimap and set/multiset are different. The elements inserted into set/multiset are separate keys, while the elements inserted into map/multimap are a pair of data composed of < key, value > pairs. The pair of data is an inseparable whole and need to use make_pair member function construction:
make_pair(k,v) / / where k represents the key and v represents the value
(2) About map/multimap element access
The iterator of map/multimap type provides two data members: one is first, which is used to access the key; The other is second, which is used to access the value. In addition, map type mappings can use keys as array subscripts to access the values corresponding to the keys, but multimap type mappings do not allow the use of array subscripts to access the elements.
A brief example of a map:
#include<bits/stdc++.h> using namespace std; int main() { string name[]={"Magnolia","Billows","Shangguan Waner"}; string pos[]={"warrior","assassin","master"}; map<string,string,greater<string>> wzry; //The default is less map<string,string>::iterator p; for(int i=0;i<3;i++) wzry.insert(make_pair(name[i],pos[i])); wzry["Ox demon"]="Tank"; wzry["Lv Bu"]="warrior"; for(p=wzry.begin();p!=wzry.end();p++) cout<<p->first<<" "<<p->second<<endl; string person; cout<<"Enter the name of the person you are looking for:"; cin>>person; for(p=wzry.begin();p!=wzry.end();p++) if(p->first==person) cout<<p->second<<endl; return 0; }
Output result:
Shangguan Waner mage Ox demon tank Lv Bu soldier LAN Assassin Mulan Warrior Enter the name of the person looking for: Shangguan Waner master
The usage of multimap and map are basically the same. The difference is that the keys in map map are not allowed to be repeated, while the keys in multimap are allowed to be repeated. In addition, map allows the subscript operation of the array to access the values in the map, while multimap is not allowed. multimap is very useful in constructing one key to many value queries.
A brief example of multimap:
#include<bits/stdc++.h> using namespace std; int main() { multimap<string,string> dict; multimap<string,string>::iterator p; string eng[]={"cliff","berg","precipice","tract"}; string che[]={"Steep cliff","iceberg","Steep cliff","One area"}; for(int i=0;i<4;i++) dict.insert(make_pair(eng[i],che[i])); //Insert a single element dict.insert(make_pair(string("tract"),string("zone"))); dict.insert(make_pair(string("precipice"),string("A dangerous situation"))); dict.insert(make_pair("day","one day")); //correct //dict["precipice"] = "cliff, cliff"; error for(p=dict.begin();p!=dict.end();p++) cout<<p->first<<" "<<p->second<<endl; string word; cout<<"Please enter the English word you want to find: "; cin>>word; for(p=dict.begin();p!=dict.end();p++) if(p->second==word) cout<<p->first<<endl; return 0; }
Output result:
berg iceberg cliff Steep cliff day one day precipice Steep cliff precipice A dangerous situation tract One area tract zone Please enter the English word you want to find: Steep cliff cliff precipice