练习11.15
mapped_type:vector<int>
key_type:int
value_type:pair<const int, vecor<int>>
练习11.16
1 int main() 2 { 3 map<string, int> m1 = { {"asd",1} }; 4 auto it = m1.begin(); 5 int n; 6 while (cin >> n) 7 { 8 it->second = n; 9 } 10 for (auto w : m1) 11 { 12 cout << w.first << w.second << endl; 13 } 14 system("pause"); 15 return 0; 16 }
练习11.17
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 10 using namespace std; 11 12 int main() 13 { 14 multiset<string> c = { "good","good","best","never","let","it","rest" }; 15 vector<string> v = { "good","good","best","never","let","it","rest" }; 16 copy(v.begin(), v.end(), inserter(c, c.end()));//正确 17 //copy(v.begin(),v.end(),back_inserter(c)); //错误 multiset没有push_back这个操作,尾插法不适合 18 //copy(c.begin(),c.end(),inserter(v,v.end()));//正确 19 //copy(c.begin(),c.end(),back_inserter(v));//正确 20 21 for (vector<string>::iterator it = v.begin(); it != v.end(); ++it) 22 cout << *it << " "; 23 cout << endl; 24 25 for (multiset<string>::iterator it = c.begin(); it != c.end(); ++it) 26 cout << *it << " "; 27 cout << endl; 28 system("pause"); 29 return 0; 30 }
练习11.18
map<string, int>::const_iterator
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 10 using namespace std; 11 12 int main() 13 { 14 map<string, int> word_count; 15 string str; 16 while (cin >> str) 17 { 18 ++word_count[str]; 19 } 20 map<string, int>::const_iterator map_it = word_count.cbegin(); 21 while (map_it != word_count.end()) 22 { 23 cout << map_it->first << " " << (*map_it).second << endl; 24 ++map_it; 25 } 26 system("pause"); 27 return 0; 28 }
练习11.19
using F = bool (const Sales_data &,const Sales_data &); multiset<Sales_data, F*>::iterator it = bookstore.begin();