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
    评论
c primer 15.9作业是关于异常处理的内容。在这一中,主要介绍了C语言中的错误处理机制和异常处理方式。 异常处理是一种程序设计中的重要思想,它允许应对出现的各种错误或异常情况,从而增加程序的健壮性和可靠性。C语言中的异常处理主要通过使用错误码和错误处理函数来实现。 在进行异常处理时,通常需要先定义一些错误码,用于标识可能出现的异常情况。C语言提供了一些标准的错误码,如errno.h头文件中定义的错误码,还可以根据需要自定义错误码。 接下来,我们需要在程序中合适的位置进行错误检测并进行异常处理。可以使用if语句或者switch语句等条件语句来检测错误码,并根据不同的错误码执行相应的错误处理代码。 错误处理代码的内容可以根据具体情况而定,它可以是打印错误信息、修复错误、返回错误码等操作。在处理完异常后,可以继续执行后续的程序逻辑,或者返回到调用处继续处理。 除了使用错误码和错误处理函数进行异常处理外,C语言还提供了一种特殊的异常处理方式,即信号处理。信号处理是通过捕捉和处理操作系统发送的信号来实现的,通过注册信号处理函数,可以在程序遇到特定信号时执行相应的处理代码。 总之,C语言中的异常处理是一种重要的错误处理机制,可以提高程序的可靠性和健壮性。通过定义错误码、错误处理函数和信号处理,可以有效地捕捉和处理各种异常情况。在编写C程序时,合理地使用异常处理机制是至关重要的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值