C++primer 12.3.2节练习

练习12.30

  1 #include <memory>
  2 #include <sstream>
  3 #include <string>
  4 #include <vector>
  5 #include <map>
  6 #include <set>
  7 #include <iostream>
  8 #include <fstream>
  9 #include <cctype>
 10 #include <cstring>
 11 #include <utility>
 12 
 13 using namespace std;
 14 
 15 using line_no = vector<string>::size_type;
 16 void runQueries(ifstream &infile);
 17 string make_plural(set<line_no>::size_type sz, string s1, string s2);
 18 class QueryResult;
 19 class TextQuery {
 20 public:
 21     using line_no = vector<string>::size_type;
 22     TextQuery(ifstream&);
 23     QueryResult query(const string&) const;
 24 private:
 25     shared_ptr<vector<string>> file;
 26     map<string, shared_ptr<set<line_no>>> wm;
 27 };
 28 
 29 class QueryResult {
 30     friend ostream &print(ostream &os,const QueryResult&);
 31 public:
 32     using line_no = vector<string>::size_type;
 33     QueryResult(string s, shared_ptr<set<line_no>> p, shared_ptr<vector<string>> f) : sought(s), lines(p), file(f) {}
 34 private:
 35     string sought;
 36     shared_ptr<set<line_no>> lines;
 37     shared_ptr<vector<string>> file;
 38 };
 39 
 40 ostream &print(ostream &os,const QueryResult &qr);
 41 
 42 int main()
 43 {
 44     ifstream in("title.txt");
 45     runQueries(in);
 46     system("pause");
 47     return 0;
 48 }
 49 
 50 string make_plural(set<line_no>::size_type sz, string s1, string s2)
 51 {
 52     if (sz != 1)
 53         return s1 + s2;
 54     else
 55         return s1;
 56 }
 57 
 58 ostream & print(ostream & os,const QueryResult & qr)
 59 {
 60     os << qr.sought << " occurs " << qr.lines->size() << " " << make_plural(qr.lines->size(), "times", "s") << endl;
 61     for (auto num : *qr.lines)
 62         os << "\t(line " << num + 1 << ") " << *(qr.file->begin() + num) << endl;
 63     return os;
 64     // TODO: 在此处插入 return 语句
 65 }
 66 
 67 void runQueries(ifstream & infile)
 68 {
 69     TextQuery tq(infile);
 70     while (true)
 71     {
 72         cout << "enter word to look for, or q to quit: ";
 73         string s;
 74         if (!(cin >> s) || s == "q") break;
 75         print(cout, tq.query(s)) << endl;
 76     }
 77 }
 78 
 79 TextQuery::TextQuery(ifstream &is) : file(new vector<string>)
 80 {
 81     string text;
 82     while (getline(is, text))
 83     {
 84         file->push_back(text);
 85         int n = file->size() - 1;
 86         istringstream line(text);
 87         string word;
 88         while (line >> word)
 89         {
 90             auto &lines = wm[word];
 91             if (!lines)
 92                 lines.reset(new set<line_no>);
 93             lines->insert(n);
 94         }
 95     }
 96 }
 97 
 98 QueryResult TextQuery::query(const string &sought) const
 99 {
100     static shared_ptr<set<line_no>> nodata(new set<line_no>);
101     auto loc = wm.find(sought);
102     if (loc == wm.end())
103         return QueryResult(sought, nodata, file);
104     else
105         return QueryResult(sought, loc->second, file);
106 }

练习12.31

set是元素唯一且排列有序的,vector不具备这些特性;

练习12.32

稍后奉上

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值