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 三.下面是逐行读取文件内容的三种方法: ...
随机推荐
- PHP 超级全局变量
超级全局变量在PHP 4.1.0之后被启用, 是PHP系统中自带的变量,在一个脚本的全部作用域中都可用. PHP中预定义了几个超级全局变量(superglobals) ,这意味着它们在一个脚本的全部作 ...
- MySQL语法语句大全
一.SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表.索引.字段等)的建立和操纵. 以下是一些重要的SQL快速参考,有关SQ ...
- 使用VisualSVN Server自动发布站点
使用VisualSVN Server自动发布站点可以节省大量的发布时间. 适用于项目测试版本 通常一个项目在测试阶段会遇到以下问题 1.开发人员修改缺陷要实时反应到测试环境 2.项目经理想让客户及时看 ...
- python之递归
递归的定义:即对自己自身内容的引用. 有用的递归函数应包含以下几步份: 当函数直接返回值时有基本的实例(最小可能性问题): 递归实例,包括一个或者多个问题较小部分的递归调用: 递归的关键就是将问题分解 ...
- AS3 - 数组Array的几个常用方法(附样例)
AS3 - 数组Array的几个常用方法(附样例) 2015-03-30 10:39发布:hangge浏览:241 Flex/Flash开发中,经常会使用到数组,下面总结了一些数组的常用方法. 1 ...
- [转] 浅谈Microsoft MVP
微软MVP,这个自1993 年开始在社群上出现的计划(MVP Award Program),目前在全球已经累积超过5,000 人,其中在台湾已经有一百多人了,包括我在内,这个计画现在已经成为以微软技术 ...
- UNIX环境高级编程-环境配置
环境配置步骤如下. 1. 下载源文件:http://www.apuebook.com/src.tar.gz. 2. 复制src.tar.gz文件到/home/me/mydir/unixl/目录(自 ...
- platform
作者yuanlulu httpblogcsdnnetyuanlulu版权没有但是转载请保留此段声明 第1章platform驱动管理机制 platform_device 数据结构 注册流程 platfo ...
- MVC的小知识点
1.MVC的前台页面编译完之后,也会生成一个前台页面类.在前天页面中加入这段代码this.GetType().Assembly.GetLocation()得到当前类所在的程序集,可以查看其所在的程序, ...
- Java 基础学习1 -- 基础语法
1.变量