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 三.下面是逐行读取文件内容的三种方法: ...
随机推荐
- 简单的下拉刷新以及优化--SwipeRefreshLayout
代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...
- The difference between Union & Union All in SQL Server/pOSTGRESQL
Following is test in SQL Server: USE [TestDB] CREATE TABLE [dbo].[UserInfoTest02]( [number] [bigint] ...
- vs2010的11个调试技巧和方法
调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...
- intellij 设置-试验过的
1.已修改的文件星号“*”标记 2.在PROJECT窗口中快速定位,编辑窗口中的文件 在编辑的所选文件按ALT+F1, 然后选择PROJECT VIEW 3.改变编辑文本字体大小 FILE -> ...
- 在Windows下忘记MySQL最高用户权限密码的解决方案
1.打开MySQL配置文件 my.ini中,添加上skip-grant-tables,可以添加到文件的末尾或者是这添加到[mysqld]的下面(直接添加在my.ini文件最后亲测可以,但是在[mysq ...
- NodeJS下访问SQL Server
1.下载node-sqlserver (1)msnodesql (msnodesql-0.2.1-v0.8-x64.msi)下载地址:下载 自行选择与自己系统相符的版本,点击安装. (2)msnod ...
- 升级iOS10之后调用摄像头/麦克风等硬件程序崩溃闪退的问题
在升级到iOS10之后, 开发过程中难免会遇到很多的坑, 下面是一些常见的坑, 我做了一些整理, 希望对大家开发有帮助: &1. 调用视频,摄像头, 麦克风,等硬件程序崩溃闪退的问题: 要注意 ...
- iOS 10的23个隐藏新特性-b
上周iOS 10正式版推送后,24小时的更新率已经超过15%,实在惊人.虽然有着初期变砖.5S6卡顿.移动VoLTE无法使用.美版无信号等BUG,但不可忽视的是,iOS 10还是带来了很多从前没有的功 ...
- Understand User's Intent from Speech and Text
http://research.microsoft.com/en-us/projects/IntentUnderstanding/ Understanding what users like to d ...
- Netty4.x中文教程系列(四) 对象传输
Netty4.x中文教程系列(四) 对象传输 我们在使用netty的过程中肯定会遇到传输对象的情况,Netty4通过ObjectEncoder和ObjectDecoder来支持. 首先我们定义一个U ...