Exercise 11.20
1#include <iostream>2#include <string>3#include <Set>4#include <map>5#include <algorithm>6#include <vector>7#include <algorithm>8#include <iterator>9 Ten using namespacestd; One A intMain () - { -map<string,int>Word_count; the stringWord; - while(Cin >>word) - { -Auto ret = Word_count.insert ({word,1 }); + if( !Ret.second) - { +++ret.first->second; A } at } - for(auto C:word_count) - { -cout << C.first <<" "<< C.second <<Endl; - } -System"Pause"); in return 0; -}
Insert is more concise in its wording.
Exercise 11.21
Disassemble it as follows
1 Auto ret = Word_count.insert ({Word,0 }); 2 ++ret.first->second;
Exercise 11.22
Parameter type
1 pair<string, vector<int>>
return type
1 pair<map<string, vector<intbool>
Exercise 11.23
1#include <iostream>2#include <string>3#include <Set>4#include <map>5#include <algorithm>6#include <vector>7#include <algorithm>8#include <iterator>9 Ten using namespacestd; One A intMain () - { -multimap<string, vector<string>>name; the stringfirst_name; - stringlname; - - while(Cin >> First_Name >>lname) + { -vector<string>last_name; + Last_name.push_back (lname); A Name.insert ({first_name, last_name}); at } - for(auto C:name) - { -cout << C.first <<" "; - for(auto W:c.second) -cout << W <<" "; incout <<Endl; - } toSystem"Pause"); + return 0; -}
C++primer 11.3.2 sessions