C++primer 11.3.6节练习

练习11.33

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <sstream>
 5 #include <set>
 6 #include <map>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <algorithm>
10 #include <iterator>
11 
12 using namespace std;
13 
14 void word_transform(ifstream &map_file, ifstream &input);
15 map<string, string> buildMap(ifstream &map_file);
16 const string &transform(const string &s, const map<string, string> &m);
17 
18 int main()
19 {
20     ifstream test1("test1.txt");
21     ifstream test2("test2.txt");
22     word_transform(test1, test2);
23     system("pause");
24     return 0;
25 }
26 
27 void word_transform(ifstream &map_file, ifstream &input)
28 {
29     auto trans_map = buildMap(map_file);
30     string text;
31     while (getline(input, text))
32     {
33         istringstream stream(text);
34         string word;
35         bool firstword = true;
36         while (stream >> word)
37         {
38             if (firstword)
39                 firstword = false;
40             else
41                 cout << " ";
42             cout << transform(word, trans_map);
43         }
44         cout << endl;
45     }
46 }
47 
48 map<string, string> buildMap(ifstream & map_file)        //将文件的转换规则放在map内
49 {
50     map<string, string> tran_map;
51     string key;
52     string value;
53     while (map_file >> key && getline(map_file, value))
54     {
55         if (value.size() > 1)
56             tran_map[key] = value.substr(1);
57         else
58             throw runtime_error("no rule for " + key);
59     }
60     return tran_map;
61 }
62 
63 const string & transform(const string & s, const map<string, string>& m)        //将字符串按照规则进行转换
64 {
65     auto map_it = m.find(s);
66     if (map_it != m.cend())
67         return map_it->second;
68     else
69         return s;
70     // TODO: 在此处插入 return 语句
71 }

练习11.34

find函数只会寻找是否有关键字,找不到返回其尾后迭代器,而下标运算符如果找不到的话就会插入到其该插入的位置;

练习11.35

效果跟原先的没有差别

练习11.36

运行会强行终止,可以这样修改

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 #include <sstream>
 5 #include <set>
 6 #include <map>
 7 #include <algorithm>
 8 #include <vector>
 9 #include <algorithm>
10 #include <iterator>
11 
12 using namespace std;
13 
14 void word_transform(ifstream &map_file, ifstream &input);
15 map<string, string> buildMap(ifstream &map_file);
16 const string &transform(const string &s, const map<string, string> &m);
17 
18 int main()
19 {
20     ifstream test1("test1.txt");
21     ifstream test2("test2.txt");
22     word_transform(test1, test2);
23     system("pause");
24     return 0;
25 }
26 
27 void word_transform(ifstream &map_file, ifstream &input)
28 {
29     auto trans_map = buildMap(map_file);
30     string text;
31     while (getline(input, text))
32     {
33         istringstream stream(text);
34         string word;
35         bool firstword = true;
36         while (stream >> word)
37         {
38             if (firstword)
39                 firstword = false;
40             else
41                 cout << " ";
42             cout << transform(word, trans_map);
43         }
44         cout << endl;
45     }
46 }
47 
48 map<string, string> buildMap(ifstream & map_file)        //将文件的转换规则放在map内
49 {
50     map<string, string> tran_map;
51     string key;
52     string value;
53     while (map_file >> key && getline(map_file, value))
54     {
55         try {
56             if (value.size() > 1)
57                 //tran_map[key] = value.substr(1);
58                 tran_map.insert({ key, value.substr(1) });
59             else
60                 throw runtime_error("no rule for " + key);
61         }
62         catch (runtime_error error)
63         {
64             cout << error.what() << endl;
65         }
66     }
67     return tran_map;
68 }
69 
70 const string & transform(const string & s, const map<string, string>& m)        //将字符串按照规则进行转换
71 {
72     auto map_it = m.find(s);
73     if (map_it != m.cend())
74         return map_it->second;
75     else
76         return s;
77     // TODO: 在此处插入 return 语句
78 }

 

posted @ 2017-08-23 14:58  五月份小姐  阅读(267)  评论(0编辑  收藏  举报