C++ primer 浅谈程序设计

设计程序的好习惯,首先是把程序所涉及的操作列出来。

分析了程序支持的任务:

以下为书中例子。

  1. It must allow the user to indicate the name of a file to process. The program will store the contents of the file so that we can display the original line in which each word appears.

 

  1. The program must break each line into words and remember all the lines in which each word appears. When it prints the line numbers, they should be presented in ascending order and contain no duplicates.

 

  1. The result of querying for a particular word should be the line numbers on which that word appeared.

 

  1. To print the text in which the word appeared, it must be possible to fetch the line from the input file corresponding to a given line number.

再分析数据结构:

以下也是书中的例子。

巧妙的用一个TextQuery类来实现这个程序。加上一些容器的使用,可以很巧妙的完成上面的任务。

  1. We'll use a vector<string> to store a copy of the entire input file. Each line in the input file will be an element in this vector . That way, when we want to print a line, we can fetch it by using the line number as an index.

 

 

  1. We'll store each word's line numbers in a set . Using a set will guarantee that there is only one entry per line and that the line numbers will be automatically stored in ascending order.

 

  1. We'll use a map to associate each word with the set of line numbers on which the word appears.
  1. 综上所述,一个TextQuery类里面有两种数据成员:存储文件的vector对象,以及一个map容器对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值