首页 / C++ / C++primer 11.1节练习
C++primer 11.1节练习
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++primer 11.1节练习,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2352字,纯文字阅读大概需要4分钟。
内容图文
练习11.1
map他是一个有序的且元素不重样的关联容器,他存放元素的方式是以键值对的方式存放的;
vector容器没有什么特别的要求
练习11.2
list适用于在在任何地方添加删除元素,因为他是个双向的链表;
vector适用于需要随机访问的程序,只能在尾后添加元素,在其他位置添加或删除元素效率会很低;代价过大
deque适用于在容器首尾添加删除元素且随机访问的程序;
map是键值对的集合,他能在查找的同时进行高效的访问;
set就是关键字的简单集合,适用于当只是想知道一个值是否存在时,是最有用的
练习11.3
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 #include <functional> 9 #include <iterator> 10 #include <fstream> 11 #include <map> 12 #include <set> 13usingnamespace std; 14usingnamespace placeholders; 1516int main() 17{ 18 ifstream in("title.txt"); 19string str; 20 map<string, size_t> word_count; 21set<string> exclude{ "a","an" }; 22while (in >> str) 23 { 24if (exclude.find(str) == exclude.end()) 25 ++word_count[str]; 26 } 27for (const auto &w : word_count) 28 { 29 cout << w.first << " occurs " << w.second << ((w.second > 1) ? "times" : "time") << endl; 30 } 31 system("pause"); 32return0; 33 }
练习11.4
1 #include<iostream> 2 #include<cctype> 3 #include<string> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <algorithm> 8 #include <list> 9 #include <functional> 10 #include <iterator> 11 #include <fstream> 12 #include <map> 13 #include <set> 14usingnamespace std; 15usingnamespace placeholders; 1617string &change(string &s); 18void delete_punct(string &s); 1920int main() 21{ 22//ifstream in("title.txt");23string str; 24 map<string, size_t> word_count; 25set<string> exclude{ "a","an" }; 26while (cin >> str) //这里读取文件就会发生错误,原因不明27 { 28if (exclude.find(str) == exclude.end()) 29 ++word_count[change(str)]; 30 } 31for (const auto &w : word_count) 32 { 33 cout << w.first << " occurs " << w.second << ((w.second > 1) ? "times" : "time") << endl; 34 } 35 system("pause"); 36return0; 37} 3839string &change(string &s) 40{ 41for (auto i = 0; i != s.size(); ++i) 42 { 43if (isupper(s[i])) 44 s[i] = tolower(s[i]); 45if (ispunct(s[i])) //这里输入带标点的会发生错误,暂时认为和cin的流输入方式有关,待解决46 s.erase(i, 1); 47 } 48return s; 49 }
原文:http://www.cnblogs.com/wuyinfenghappy/p/7367391.html
内容总结
以上是互联网集市为您收集整理的C++primer 11.1节练习全部内容,希望文章能够帮你解决C++primer 11.1节练习所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。