第十一章 关联容器
Posted GGBeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十一章 关联容器相关的知识,希望对你有一定的参考价值。
11.1
map:存储关键字-值(key-value)对的容器,关键字起到索引的作用,值则表示与索引相关联的数据;关键字是唯一、有序存储的
vector:存储单一类型元素的容器;元素是按添加顺序存储的
11.2
list:频繁在任意处插入/删除元素但不要求有随机访问能力时
vector:在尾部插入/删除元素且要求具有随机访问能力时
deque:在首尾插入/删除元素且要求具有随机访问能力时
map:统计每个单词在输入中出现的次数
set:查找集合中是否包含某个数
11.3
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 #include <map> 14 #include <set> 15 16 using namespace std; 17 using namespace std::placeholders; 18 19 int main() 20 { 21 map<string, int> jzd; 22 string word; 23 while (cin >> word) { 24 ++jzd[word]; 25 } 26 for (auto &i : jzd) { 27 cout << i.first << " occurs about " << i.second << (i.second > 1 ? "times.\n" : "time\n"); 28 } 29 return 0; 30 }
11.4
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 #include <map> 14 #include <set> 15 #include <cctype> 16 17 using namespace std; 18 using namespace std::placeholders; 19 20 int main() 21 { 22 map<string, int> jzd; 23 string word; 24 while (cin >> word) { 25 for (auto &i : word) 26 if (i <= ‘Z‘ && i >= ‘A‘) i = tolower(i); 27 for (auto it = word.begin(); it != word.end(); ++it) { 28 if (ispunct(*it)) { 29 it = word.erase(it); 30 --it; 31 } 32 } 33 ++jzd[word]; 34 } 35 for (auto &i : jzd) { 36 cout << i.first << " occurs about " << i.second << (i.second > 1 ? "times.\n" : "time\n"); 37 } 38 return 0; 39 }
11.5
map:每个元素都是一个关键字-值对,初始化时必须提供关键字类型和值类型
set:每个元素只是一个关键字,没有值,元素类型就是关键字类型
11.6
set:元素都是唯一有序的
list:插入/删除元素很方便
11.7
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 #include <map> 14 #include <set> 15 #include <cctype> 16 17 using namespace std; 18 using namespace std::placeholders; 19 20 int main() 21 { 22 map<string, vector<string>> imap; 23 string name1, name2; 24 while (cin >> name1) { 25 cin >> name2; 26 imap[name1].push_back(name2); 27 } 28 for (auto &i : imap) { 29 for (auto &j : i.second) { 30 cout << i.first << " " << j << endl; 31 } 32 cout << endl; 33 } 34 return 0; 35 }
11.8
1 #include <iostream> 2 #include <fstream> 3 #include <iterator> 4 #include <vector> 5 #include <string> 6 #include <deque> 7 #include <list> 8 #include <forward_list> 9 #include <stack> 10 #include <queue> 11 #include <algorithm> 12 #include <functional> 13 #include <map> 14 #include <set> 15 #include <cctype> 16 17 using namespace std; 18 using namespace std::placeholders; 19 20 void use_set(const vector<string> &vec) 21 { 22 set<string> iset(vec.begin(), vec.end()); 23 for (auto &i : iset) cout << i << " "; 24 cout << endl; 25 } 26 27 void use_vector(const vector<string> &vec) 28 { 29 vector<string> ivec(vec.begin(), vec.end()); 30 //unique必须要先sort过 31 sort(ivec.begin(), ivec.end()); 32 vector<string>::iterator it = unique(ivec.begin(), ivec.end()); 33 ivec.erase(it, ivec.end()); 34 for (auto &i : ivec) cout << i << " "; 35 cout << endl; 36 } 37 38 int main() 39 { 40 vector<string> vec = {"scout", "peanut", "jack", "peanut", "amazon", "jack", "demo"}; 41 use_set(vec); 42 use_vector(vec); 43 return 0; 44 }
11.9
以上是关于第十一章 关联容器的主要内容,如果未能解决你的问题,请参考以下文章