c++ primer 学习笔记10--关联容器
2015-04-12 14:42
621 查看
书本376页。练习11.4:单词计数,忽略大小写,与标点。
练习11.7
定义一个map,关键字是家庭的姓,保存的是孩子的名。实现添加新的家庭以及向已有的家庭添加孩子。
11.9,定义一个map,将单词与一个行号的list关联,list保存的是单词所在的行号。
#include <iostream> #include <vector> #include <string> #include <fstream> #include <map> #include <algorithm> using namespace std; string& trans(string &s) { for (int p = 0; p < s.size(); ++p) { if (s[p] >= 'A'&& s[p] <= 'Z') { s[p] -= 'A' - 'a'; } else if (s[p] == ',' || s[p] == '.') { s.erase(p, 1); } } return s; } int main(int argc,char *argv[]) { ifstream in(argv[1]); if (!in) { cerr << "打开输入文件失败" << endl; return -1; //exit(1); } map<string, size_t> word_count; string word; while (cin >> word) { ++word_count[word]; } for (auto &w : word_count) { cout << w.first << "出现了" << w.second << "次" << endl; } }
练习11.7
定义一个map,关键字是家庭的姓,保存的是孩子的名。实现添加新的家庭以及向已有的家庭添加孩子。
#include <iostream> #include <vector> #include <string> #include <fstream> #include <map> #include <algorithm> using namespace std; void add_family(map<string, vector<string>> &families, const string &family) { if (families.find(family) != families.end()) { families[family] = vector<string>(); } } void add_child(map<string, vector<string>> &families, const string &family,const string &child) { families[family].push_back(child); } int main(int argc,char *argv[]) { map<string, vector<string>> families; add_family(families, "张"); add_child(families, "张", "强"); add_child(families, "张", "刚"); add_child(families, "王", "五"); add_family(families, "王"); for (auto f : families) { cout << f.first << "家的孩子"; for (auto c : f.second) { cout << c << " "; } cout << endl; } }
11.9,定义一个map,将单词与一个行号的list关联,list保存的是单词所在的行号。
#include <iostream> #include <vector> #include <string> #include <fstream> #include <map> #include <algorithm> #include <list> #include <sstream> using namespace std; string& trans(string &s) { for (int p = 0; p < s.size(); ++p) { if (s[p] >= 'A'&& s[p] <= 'Z') { s[p] -= 'A' - 'a'; } else if (s[p] == ',' || s[p] == '.') { s.erase(p, 1); } } return s; } int main(int argc, char *argv[]) { ifstream in(argv[1]); if (!in) { cerr << "打开输入文件失败" << endl; return -1; //exit(1); } map<string, list<int>> word_lineno; string line; string word; int lineno = 0; while (getline(in, line)) { ++lineno;//行号增加 istringstream l_in(line);//构造字符串流,读取单词 while (l_in >> word) { trans(word); word_lineno[word].push_back(lineno); } } for (auto &w : word_lineno) { cout << w.first << "所在行: " ; for (auto i : w.second) { cout << i << " "; } cout << endl; } }11.12.编写程序,读入string 和int的序列,将每个string 和int存入一个pair中,pair保存在一vector中
#include <iostream> #include <vector> #include <string> #include <fstream> #include <map> #include <algorithm> using namespace std; int main(int argc, char *argv[]) { ifstream in(argv[1]); if (!in) { cerr << "打开输入文件失败" << endl; return -1; //exit(1); } vector<pair<string, int>> data; string s; int v; while (in >> s && in >> v) { data.push_back(pair<string, int>(s, v)); //data.push_back(make_pair(s,v)); //data.push_back({s,v}); } for (auto &w : data) { cout << w.first << " " << w.second << endl; } }
相关文章推荐
- 【C++ Primer 学习笔记】 chapter 10 关联容器
- 《c++ primer》 第11章 关联容器 学习笔记
- 【C++ Primer】【学习笔记】【第十章】关联容器之:map类型
- 关联容器 - 1【C++ Primer 学习笔记 - 第十章】
- 【C++ Primer 学习笔记】: 容器和算法之【关联容器】
- C++ Primer 学习笔记——关联容器初识
- C++ Primer学习之(10)——关联容器
- c++ primer(第五版)学习笔记及习题答案代码版(第十一章)关联容器
- C++ Primer学习笔记2--c++ 关联容器
- 【C++ Primer】【学习笔记】【第十章】关联容器之:set类型
- C++ Primer 学习笔记:关联容器入门 set && map
- 【C++ Primer】【学习笔记】【第十章】关联容器之:文本查询程序
- C++ primer第二次阅读学习笔记(第10章:关联容器)
- C++ Primer 学习笔记_34_STL实践与分析(8) --引言、pair类型、关联容器
- c++ primer 学习笔记18 关联容器
- C++ Primer 学习笔记——关联容器的操作
- 【C++ Primer】【学习笔记】【第十章】关联容器之:multimap和multiset类型
- C++ Primer 学习笔记_34_STL实践与分析(8) --引言、pair类型、关联容器
- 【C++ Primer】【学习笔记】【第十章】关联容器之:pair类型
- 顺序容器 - 1【C++ Primer 学习笔记 - 第九章】