C++primer 11.1节练习

练习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>
13 using namespace std;
14 using namespace placeholders;
15 
16 int main()                
17 {
18     ifstream in("title.txt");
19     string str;
20     map<string, size_t> word_count;
21     set<string> exclude{ "a","an" };
22     while (in >> str)
23     {
24         if (exclude.find(str) == exclude.end())
25             ++word_count[str];
26     }
27     for (const auto &w : word_count)
28     {
29         cout << w.first << " occurs " << w.second << ((w.second > 1) ? "times" : "time") << endl;
30     }
31     system("pause");
32     return 0;
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>
14 using namespace std;
15 using namespace placeholders;
16 
17 string &change(string &s);
18 void delete_punct(string &s);
19 
20 int main()                
21 {
22     //ifstream in("title.txt");
23     string str;
24     map<string, size_t> word_count;
25     set<string> exclude{ "a","an" };
26     while (cin >> str)        //这里读取文件就会发生错误,原因不明
27     {
28         if (exclude.find(str) == exclude.end())
29             ++word_count[change(str)];
30     }
31     for (const auto &w : word_count)
32     {
33         cout << w.first << " occurs " << w.second << ((w.second > 1) ? "times" : "time") << endl;
34     }
35     system("pause");
36     return 0;
37 }
38 
39 string &change(string &s)
40 {
41     for (auto i = 0; i != s.size(); ++i)
42     {
43         if (isupper(s[i]))
44             s[i] = tolower(s[i]);
45         if (ispunct(s[i]))            //这里输入带标点的会发生错误,暂时认为和cin的流输入方式有关,待解决
46             s.erase(i, 1);
47     }
48     return s;
49 }

 

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7367391.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值