本题目对应于 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(读取文本)的更多相关文章

  1. MySQL中游标使用以及读取文本数据

    原文:MySQL中游标使用以及读取文本数据 前言 之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课.当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试 ...

  2. Python读取文本,输出指定中文(字符串)

    因业务需求,需要提取文本中带有检查字样的每一行. 样本如下: 1 投入10kVB.C母分段820闭锁备自投压板 2 退出10kVB.C母分段820备投跳803压板 3 退出10kVB.C母分段820备 ...

  3. C#读取文本播放相应语音【转】

    第一种方案: 利用微软text to speech引擎(TTS),读取文本 (1)添加Microsoft Speech Object Library的项目引用 (2)引入using SpeechLib ...

  4. Python循环文件推荐的方式,可用于读取文本最后一行或删除指定行等

    读取文本最后一行: f = open('test11.txt', 'rb') for i in f: offset = -16 while True: f.seek(offset, 2) data = ...

  5. Python实现随机读取文本N行数据

    工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...

  6. 解决FileInputStream读取文本时 最后端会多出字符问题

    使用 read(byte[]) 方法读取文本的时候,要用 String str = new String(byte[],int offset,int len) 来将数组中的元素转换为String字符串 ...

  7. c++ 读取文本问题

    c++文本操作有以下三个方法 ifstream,ofstream,fstream 读取文本常用的方法如下 std::ifstream input; input.open(".log" ...

  8. js读取文本内容,支持csv.txt

    js读取文本内容,支持csv.txt <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  9. python逐行读取文本

    一.使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. 二.需要导入import os 三.下面是逐行读取文件内容的三种方法: ...

随机推荐

  1. 解决iOS设备屏幕切换时页面造成的问题

    环境:IOS6~7 Safari 问题:iOS设备屏幕切换时可能会造成屏幕变大,出现左右间距等问题 解决方法: 头部加入<meta name = "viewport" con ...

  2. 【转】MyEclipse快捷键大全(绝对全)

    ref:http://www.douban.com/note/254195820/?type=like# ctrl+w 关闭单个窗口 F3 跳转到类.变量的声明 F11 运行上次程序 Ctrl + F ...

  3. C# 获取汉字的拼音首字母

    /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...

  4. 将double类型的值保留几位小数

    1.第一个参数(3.1415926)是要处理的数值.第二个参数(1)为要保留的几位小数.第三个参数是按照“四舍五入”还是"直接取这一位的值"(MidpointRounding.To ...

  5. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...

  6. 纯css3的上下左右提示框几种方法

    经常用到三角形提示框,用图片吧,大小框不定,所以,css自己写了,可设置宽高比,就可自适应了. 图形例子如下: css代码如下 <style type="text/css"& ...

  7. CentOS 6.4 下搭建 MongoDB 2.4.9 环境

    一.下载MongoDB2.4.9版 下载MongoDB wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz 解压Mo ...

  8. hadoop集群默认配置和常用配置【转】

    转自http://www.cnblogs.com/ggjucheng/archive/2012/04/17/2454590.html 获取默认配置 配置hadoop,主要是配置core-site.xm ...

  9. [原创]PostgreSQL Plus Advanced Server配合crontab实现定时维护工作

    本文要实现的目标1.PostgreSQL定时全备2.定时删除归档WAL文件3.定时删除全备文件4.删除n天之前的数据库日志文件 实验环境操作系统:RHEL 6.3数据库:PostgreSQL Plus ...

  10. python之量的概念

    程序设计语言: 量的类型: 1. 直接量 2. 常量 3. 变量 量的因素: 1. 存储类别 2. 数据类型 3. 作用域 4. 生存期