C++ prime 第十一章
2021/10/30 17:09:39
本文主要是介绍C++ prime 第十一章,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
11.4
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> using namespace std; using namespace placeholders; void process(string& s) { for (auto i = s.begin(); i != s.end(); ++i) { *i = tolower(*i); //处理大写字母 s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号 } } int main(int argc, char** argv) { map<string, size_t> word_count; string word; while (cin >> word) { process(word); ++word_count[word]; } for (const auto &w : word_count) { cout << w.first << " " << w.second << ((w.second > 1) ? "times" : "time") << endl; } return 0; }
10.7
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> using namespace std; using namespace placeholders; void process(string& s) { for (auto i = s.begin(); i != s.end(); ++i) { *i = tolower(*i); //处理大写字母 s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号 } } int main(int argc, char** argv) { map<string, vector<string>> family; string last_name, child_name; cout << "请输入家族姓氏" << endl; cin >> last_name; cout << "请输入孩子的名字" << endl; while (cin >> child_name) { family[last_name].push_back(child_name); } for (auto i : family) { cout << "该姓氏 : " << i.first << "成员有 : " << endl; for (auto j : i.second) { cout << j << " "; } cout << endl; } return 0; }
11.12
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> #include<utility> using namespace std; using namespace placeholders; /*void process(string& s) { for (auto i = s.begin(); i != s.end(); ++i) { *i = tolower(*i); //处理大写字母 s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号 } }*/ int main(int argc, char** argv) { vector<pair<string, int>> vec; string s; int num; cout << "请输入要读入的字符串 和 数字" << endl; while (cin >> s >> num) { vec.push_back(pair<string, int>(s, num)); //第一种 vec.push_back({ s,num }); //第二种 vec.emplace_back(s, num); //第三种 vec.push_back(make_pair(s, num)); //第四种 } for (auto i : vec) { cout << i.first << " " << i.second << endl; } return 0; }
11.14
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> using namespace std; using namespace placeholders; //void process(string& s) //{ // for (auto i = s.begin(); i != s.end(); ++i) // { // *i = tolower(*i); //处理大写字母 // s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号 // // } //} int main(int argc, char** argv) { map<string, vector<pair<string,string>>> family; string last_name, child_name, birthday; cout << "请输入家族姓氏" << endl; cin >> last_name; cout << "请输入孩子的名字和生日" << endl; while (cin >> child_name >> birthday) { family[last_name].push_back({ child_name,birthday }); } for (auto i : family) { cout << "该姓氏 : " << i.first << "成员有 : " << endl; for (auto j : i.second) { cout << j.first << " " << j.second; } cout << endl; } return 0; }
11.31
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> using namespace std; using namespace placeholders; /*void process(string& s) { for (auto i = s.begin(); i != s.end(); ++i) { *i = tolower(*i); //处理大写字母 s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号 } }*/ int main(int argc, char** argv) { multimap<string, string> mp = { {"Joker","renzhendexue"},{"Joker","yanyuan"},{"Joker","weibeideqingchun"} ,{"Jony J","nuli"}}; auto iter = mp.find("Joker"); auto num = mp.count("Joker"); while (num) { if (iter->second == "renzhendexue") { mp.erase(iter); break; } ++iter; --num; } for (auto i : mp) { cout << i.first << "的作品" << i.second << endl; } cout << endl; system("pause"); cin.get(); }
11.32
#include<iostream> #include<string> #include<fstream> #include<sstream> #include<vector> #include<forward_list> #include<deque> #include<algorithm> #include<list> #include<functional> #include<iterator> #include<map> #include<set> #include<unordered_map> #include<unordered_set> #include<cctype> using namespace std; using namespace placeholders; int main(int argc, char** argv) { multimap<string, string> mp = { {"Joker","renzhendexue"},{"Joker","yanyuan"},{"Joker","weibeideqingchun"} ,{"Jony J","nuli"}}; auto iter = mp.find("Joker"); auto num = mp.count("Joker"); map<string, set<string>> ans; //对于set,系统能根据元素的值自动进行排序 for (auto i : mp) { ans[i.first].insert(i.second); } for (auto i : ans) { cout << i.first << " : "; for (auto j : i.second) { cout << j << " "; } cout << endl; } system("pause"); return 0; cin.get(); }
这篇关于C++ prime 第十一章的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享