The associated containers in C ++ are map, set, multimap, and Multiset.
In order to talk about map, we must first combine pair: pair as a combination of two types. For example, a student ID can be pair <int, string>, where Int Is the student ID, string is the name.
Map is a container, which contains several pair. The first element of each pair is called a key (note that the key type must support the <) Operation !), The second element is called a value. For a common map, each key corresponds to a value. As a result, the key is similar to the subscript of the array, and the value is similar to the value of the array. The map class defines three types: key_type, mapped_type, and vaule_type. They indicate the key type, value type, and a pair type respectively. The first element of pair is the key, and the second element is the value.
First, let's talk about how to add elements to IMAP: There are two ways to use keys (subscripts) or insert
This method uses subscript conflicts with containers of vector and other types. If it is an empty vector, you cannot directly access the element using the subscript. You must put pusn_back into the element. If you access the element directly, an error is returned. If map does not have this key (subscript), it will automatically add this key to map with a value of 0. The Return Value of the subscript operation is the value associated with this key. With this feature, we can conveniently complete the statistical function. For example:
int main(){string str;map<string,int> wordCount;while(cin>>str){++wordCount[str];}map<string,int>::iterator it_map = wordCount.begin();cout<<"word"<<"\t\t"<<"count"<<endl;for(;it_map != wordCount.end();++it_map)cout<<it_map->first<<"\t\t"<<it_map->second<<endl;return 0;}
If the key does not exist during use, the value is 0 and then auto-incrementing. It is changed to 1.
The insert method has multiple overload functions, indicating whether you insert a pair, a pair of pair specified by the iterator, or insert a pair to the specified position, however, a pair is usually inserted, and the key of this function is its return value. The returned value is also a pair. The first element of pair is an iterator pointing to the map type, and the other is a Boolean variable, indicating whether the insert is successful or not. You can use insert to overwrite the above program:
Int main () {string STR; Map <string, int> wordcount; while (CIN> Str) {// For each word, both try to insert it pair <Map <string, int >:: iterator, bool> ret = wordcount. insert (make_pair (STR, 1); // you can check the returned value to determine whether the insert operation is successful. If (! Ret. second) // insertion failure indicates that the map has this word. You only need to increase the value of the corresponding key to ++ ret. first-> second;} Map <string, int >:: iterator it_map = wordcount. begin (); cout <"word" <"\ t" <"count" <Endl; For (; it_map! = Wordcount. End (); ++ it_map) cout <it_map-> first <"\ t" <it_map-> second <Endl; return 0 ;}
So how to read the map elements? Although we can also use a key to read data, the potential problem is that if the key does not exist, it will be automatically created. This feature is not what we want. We can use the count or find function to check whether a key exists. The difference between the two functions is that count returns the number of occurrences (MAP can only be 0 or 1), while find returns the iterator pointing to the key (if not found, returns the superterminal iterator :. end ()). This means that you can use count to determine whether an element exists. If you find an element and want to use it, find is suitable.
The delete element uses the erase operation, which is slightly different from the sequential container. However, note that the order of the associated containers is arranged by the "key.
The following is a small program:
# Include <iostream> # include <map> # include <string> # include <vector> using namespace STD; int main () {string STR; Map <string, int> wordcount; while (CIN> Str) {// For each word, try inserting it pair <Map <string, int>: iterator, bool> ret = wordcount. insert (make_pair (STR, 1); // you can check the returned value to determine whether the insert operation is successful. If (! Ret. second) // insertion failure indicates that the map has this word. You only need to increase the value of the corresponding key to ++ ret. first-> second;} Map <string, int >:: iterator it_map = wordcount. begin (); cout <"word" <"\ t" <"count" <Endl; For (; it_map! = Wordcount. end (); ++ it_map) cout <it_map-> first <"\ t" <it_map-> second <Endl; // count method: for map, 1 or 0int CNT = 0; CNT = wordcount is returned. count ("bird"); cout <"CNT" <CNT <Endl; // find method: return the iterator pointing to the key int occurs = 0; map <string, int >:: iterator it = wordcount. find ("bird"); If (it! = Wordcount. end () occurs = it-> second; cout <"occurs =" <occurs <Endl; // Delete the element int del; string S1 = "hate "; // use the value to delete del = wordcount. erase (S1); If (DEL) cout <S1 <"has been removed! "<Endl; elsecout <" can't find the word! "<Endl; // use iterator to delete Map <string, int>: iterator iter = wordcount. Begin (); wordcount. Erase (ITER); Return 0 ;}
After map is finished, let's look at set. Set has only a key and no value, so its vaule_type is not the pair type, but the key_type type. Similarly, it supports insert, find, and count operations. MAP is suitable for storing the corresponding key values, and set is suitable for storing keys. The judgment keys are not in this set, such as blacklists.
The relationship between keys and values is unique. Multimap or Multiset supports one-to-multiple relationships. And for the same key, it is always stored continuously. Because of this one-to-multiple relationship, multimap and Multiset support operations that are not available in map and set. For example, lower_bound, upper_bound, and performance_range. They respectively return the first element pointing to a key, the next element of the last element, and the range of the connected element. Let's look at a comprehensive example:
# Include <iostream> # include <map> # include <string> using namespace STD; int main () {multimap <string, string> authors; string author, work, searchitem; // create the container do {cout <"Enter authors name" <Endl; CIN> author; If (! CIN) break; cout <"Enter authors works" <Endl; while (CIN> Work) authors. insert (make_pair (author, work); cin. clear () ;}while (CIN); cin. clear (); // Delete the cout element <"who is the author that you want erase:" <Endl; CIN> searchitem;/* // use erase to delete: output all values of the corresponding key: multimap <string, string >:: iterator iter = authors. find (searchitem); If (ITER! = Authors. End () Authors. Erase (searchitem); elsecout <"cannot find the author! "<Endl; * // use the performance_range or iterator to delete typedef multimap <string, string >:: iterator ittype; pair <ittype, ittype> Pos = authors. performance_range (searchitem); If (POS. first! = Pos. Second) Authors. Erase (Pos. First, POS. Second); elsecout <"can not find this author! "<Endl; // output the delete result cout <" author \ t \ twork: "<Endl; multimap <string, string >:: iterator itbegin = authors. begin (); For (; itbegin! = Authors. End (); ++ itbegin) cout <itbegin-> first <"\ t" <itbegin-> second <Endl; return 0 ;}