C++Primer课后题10.14、10.20、11.7
来源:互联网 发布:造价软件破解版 编辑:程序博客网 时间:2024/06/18 11:41
//习题10.14void output_words(vector<string> &words){ for (auto i : words) { cout << i << " "; } cout << endl;}void elimDups(vector<string> &words){ sort(words.begin(), words.end()); auto end_unique = unique(words.begin(), words.end()); words.erase(end_unique, words.end()); }void biggies(vector<string> &words, vector<string>::size_type sz){ elimDups(words); stable_sort(words.begin(), words.end(), [](const string a, const string b) { return a.size() < b.size(); }); auto wc = find_if(words.begin(), words.end(), [sz](const string &a) { return a.size() >= sz; }); auto count = words.end() - wc; cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl; for_each(wc, words.end(), [](const string & s) {cout << s << " "; }); cout << endl;}int main(){ vector<string> words; string word; ifstream in("data.txt"); while (in >> word) words.push_back(word); biggies(words, 4); return 0;}//---------------------------------------------------//习题10.20int main(){ int sz = 0; ifstream in("data.txt"); if (!in) { cout << "打开失败" << endl; return -1; } vector<string> words; string word; while (in >> word) words.push_back(word); cout << "文本内容:"; for (auto j : words) cout << j << " "; cout << endl; while (1) { cout << "想要知道单词大于几的个数?" << endl; cin >> sz; auto i = count_if(words.begin(), words.end(), [sz](const string &str) {return str.size() > sz; }); cout << "大于" << sz << "的单词个数为:" << i << endl; }}//-------------------------------------------------bool check_size(const string &str1, string::size_type sz){ return str1.size() >= sz;}int main(){ int sz = 5; ifstream in("data.txt"); if (!in) { cout << "打开失败" << endl; return -1; } vector<string> words; string word; while (in >> word) words.push_back(word); cout << "文本内容:"; for (auto j : words) cout << j << " "; cout << endl; auto bc = count_if(words.begin(), words.end(), bind(check_size, _1, sz)); cout << bc << endl; }//--------------------------------------//习题11.7void add_family(map<string, vector<pair<string, string>>> &families, const string &family ){ if (families.find(family) != families.end()) cout << "已经有该家庭,不能再添加" << endl; else families[family];}void add_child(map<string, vector<pair<string, string>>> &families, const string &family, const string &child, const string &birthday){ families[family].push_back({ child, birthday });}int main(){ map<string, vector<pair<string, string>>> families; add_family(families, "张"); add_family(families, "李"); add_family(families, "张"); add_child(families, "张", "学良", "1923-1-2"); add_child(families, "张", "学铭", "1924-2-4"); add_child(families, "赵", "学思", "1929-2-19"); for (auto f : families) { cout << f.first << "家的孩子:"; for (auto c : f.second) cout << c.first << "(生日" << c.second << "),"; cout << endl; }}
0 0
- C++Primer课后题10.14、10.20、11.7
- C++primer plus第六版课后编程题答案11.7
- c++primer第十章课后编程题
- C++Primer课后题11.0、11.31
- C++primer函数部分课后题
- C++Primer Chapter3课后习题
- C++primer plus第六版课后编程练习答案11.7
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- Apache2.4(二) 虚拟web服务器配置
- laravel-5.3(1) 安装
- angular 解意 网址
- Android内存泄露监测工具之leakcanary
- LeetCode 318. Maximum Product of Word Lengths
- C++Primer课后题10.14、10.20、11.7
- MySQL 子查询-行、表
- jQuery 添加与删除
- iSCSI
- 远程代答平台的功能与作用
- Ubuntu 安装注意事项
- 我的hibernate学习之旅(3)~通过给类文件添加注解的方式来配置映射
- opencv3学习笔记(三)——core组件之模块operations on array
- javascript 正则表达式 候选