C ++ primer Study Notes: Use of insert operations on map containers -- compile a program to count and output the number of times the words are read.

Source: Internet
Author: User

The insert function version in the map container that contains one or a pair of iteration parameters does not indicate whether or how many elements are inserted into the container. In a single parameter version, the pair type object is returned:

 

M. insert (e) E is a value of the value_type used on M. If the key (E. First) is not in m, a new element with the value of E. Second is inserted. If the key already exists in m, m remains unchanged. This function returns a pair type object, which contains the map iterator pointing to the element with the key E. First and a bool type object, indicating whether the element is inserted.

 

I feel that the use of insert here is hard to understand, so as not to forget it.

 

Compile a program to count and output the number of times the read words appear

#include <iostream>#include <string>#include <map>using namespace std;int main(){string word;map<string,int> word_count;while(cin >> word){pair<map<string,int>::iterator,bool> p = word_count.insert(make_pair(word,1));if(p.second == false){++p.first->second;}}for(map<string,int>::iterator flag=word_count.begin();flag!=word_count.end();flag++)cout << flag->first << " " << flag->second << endl;}

 

Analysis:

 

pair<map<string,int>::iterator,bool> p = word_count.insert(make_pair(word,1));

 

P is a pair variable. The first element is the map <string, int> container iterator, and the second element is the bool type. The insert operation adds an object with the string word key and the int 1 value to the word_count container. The insert operation returns pair to P, the first element iterator of P points to string word, And when word does not exist in word_count, the second element of P is true, and the existence is false. If (P. Second = false) is used to determine that if a word exists in word_count, the operation in the IF statement is executed.

 

++p.first->second;

 

The statement can be split

++(*(p.first).second);

P. first is the iterator pointing to the string word object in the word_count container. By referencing the iterator, The word_count container's key is the string word object. The object type is Map <string, int>:: valut_type. Value_type is of the pair type. Run. Second on the object to obtain that the second element type is int. In this program, it is the number of word occurrences. Finally, perform the auto-increment operation on the second element of the int type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.