C++primer中的TextQuery(读取文本)
本题目对应于 C++primer(第四版)中 第十章的文本查询类(TextQuery)
用到的知识有:
顺序容器 vector
关联容器 set,map
标准输入输出流,文件流,字符串流
//写一个文件 从文件中读单词 输出单词的次数和所在的行数
//...
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <set>
#include <vector>
#include <map> class TextQuery
{
public:
typedef std::vector<std::string>::size_type line_no;
//read the file
void read_file(std::ifstream &is){
store_file(is);
build_map();
}
//query the file return the times every word appears
std::set<line_no> run_query(const std::string&) const; //return the line-number of the word
std::string text_line(line_no) const; private:
void store_file(std::ifstream &is);
void build_map(); std::vector<std::string> lines_of_text;
std::map<std::string, std::set<line_no>> word_map;
}; void TextQuery::store_file(std::ifstream &is){
std::string textline;
while(std::getline(is, textline)){
lines_of_text.push_back(textline);
}
} void TextQuery::build_map(){
for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num){
std::istringstream line(lines_of_text[line_num]);
std::string word;
while(line >> word){
word_map[word].insert(line_num);
}
}
} //read to get times of every word appears
std::set<TextQuery::line_no> TextQuery::run_query(const std::string& query_word) const{
std::map<std::string, std::set<line_no>>::const_iterator loc = word_map.find(query_word); if(loc == word_map.end()){
return std::set<line_no>();
}
else{
return loc->second;
}
} //read to get the line-number of the word
std::string TextQuery::text_line(line_no line) const{
if(line < lines_of_text.size()){
return lines_of_text[line];
throw std::out_of_range("line number out of range");
} }; std::string make_plural(size_t ctr, const std::string& word, const std::string& ending){
return (ctr == 1) ? word : word + ending;
} void print_results(const std::set<TextQuery::line_no>& locs, const std::string& sought, const TextQuery& file){
typedef std::set<TextQuery::line_no> line_nums;
line_nums::size_type size = locs.size(); std::cout<< "\n" << sought << "occurs " << size << " " << make_plural(size, "time", "s") << std::endl; line_nums::const_iterator it = locs.begin();
for(; it != locs.end(); ++it){
std::cout << "\t(line" << (*it) + 1 << ")" <<file.text_line(*it) << std::endl;
}
} int main(){ std::ifstream infile("F:\\input.txt");
TextQuery tq;
tq.read_file(infile); while(true){
std::cout << "enter word to look for, or q to quit: ";
std::string s;
std::cin >> s; if(!std::cin || s == "q") break;
// get the set of line numbers on which this word appears
std::set<TextQuery::line_no> locs = tq.run_query(s);
print_results(locs, s, tq); }
return 0;
}
C++primer中的TextQuery(读取文本)的更多相关文章
- MySQL中游标使用以及读取文本数据
原文:MySQL中游标使用以及读取文本数据 前言 之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课.当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试 ...
- Python读取文本,输出指定中文(字符串)
因业务需求,需要提取文本中带有检查字样的每一行. 样本如下: 1 投入10kVB.C母分段820闭锁备自投压板 2 退出10kVB.C母分段820备投跳803压板 3 退出10kVB.C母分段820备 ...
- C#读取文本播放相应语音【转】
第一种方案: 利用微软text to speech引擎(TTS),读取文本 (1)添加Microsoft Speech Object Library的项目引用 (2)引入using SpeechLib ...
- Python循环文件推荐的方式,可用于读取文本最后一行或删除指定行等
读取文本最后一行: f = open('test11.txt', 'rb') for i in f: offset = -16 while True: f.seek(offset, 2) data = ...
- Python实现随机读取文本N行数据
工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...
- 解决FileInputStream读取文本时 最后端会多出字符问题
使用 read(byte[]) 方法读取文本的时候,要用 String str = new String(byte[],int offset,int len) 来将数组中的元素转换为String字符串 ...
- c++ 读取文本问题
c++文本操作有以下三个方法 ifstream,ofstream,fstream 读取文本常用的方法如下 std::ifstream input; input.open(".log" ...
- js读取文本内容,支持csv.txt
js读取文本内容,支持csv.txt <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- python逐行读取文本
一.使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. 二.需要导入import os 三.下面是逐行读取文件内容的三种方法: ...
随机推荐
- jQuery插件css3动画模拟confirm弹窗
相比浏览器自带的alert.confirm,能力所及,我更喜欢所有的东西都是自定义:首先在head标签(当然喜欢其他地方自己看着办)内引入插件样式表和js.<link rel="sty ...
- 安装配置 redis
1. cd /usr/ley/softwares 2. wget http://download.redis.io/redis-stable.tar.gz 3. tar –xzf redis- ...
- linux下配置Apache基于加密的认证访问
1.首先要确认安装了 mod_ssl模块 我的机器是centos是系统,执行下面命令 yum install -y mod_ssl 2.用openssl工具生成密钥,证书请求文件,证书 在/usr/l ...
- 不再用.NET框架
.NET 平台很棒.真的很棒.直到它不再那么棒.我为什么不再用 .NET?简单来说,它限制了我们选择的能力(对我来说很重要),转移了我们的注意力,使得我们向内认知它的安全性,替代了帮助我们认知外面广阔 ...
- C#项目连接数据库的配置
一:C# 连接SQL数据库 1.用SqlServer数据库,windows身份验证模式<add name="TestSqlSqever" providerName=&q ...
- 小课堂Week11 会说话的代码
小课堂Week11 会说话的代码 今天主要讨论下,在编码过程中和"命名"相关的问题.因为命名方法比较自由,如果要提高可读性,我们需要尽量使其符合正规的英文语法习惯. 变量/属性 通 ...
- openerp 经典收藏 workflow中的‘非典型’自动触发器trigger_model(转载)
workflow中的‘非典型’自动触发器trigger_model 原文:http://cn.openerp.cn/workflow%E4%B8%AD%E7%9A%84%E9%9D%9E%E5%85% ...
- .NET基础之--C#中判断空字符串的3种方法性能分析
那么为什么if(a.Length==0)最快呢?因为整数判断等于最快,没有经过实例化等复杂的过程. 所以:建议大家判断字符串是否为空用 if(a.Length==0). 对于三种方法的评价: 1.if ...
- nodejs小问题:express不是内部或外部命令(转载)
安装express之后发现居然提示express不是内部或外部命令. 工具/原料 Node.js安装包 方法/步骤 1 首先下载Node.js安装包,此处我用的是官方最新的v0.10.27 32位版: ...
- 20145120 《Java程序设计》第5周学习总结
20145120 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch语法,如果被try{}的语句出现了catch()的问题就执行catch{}的语句. 错误的对象都继 ...