TextQuery程序

我写的第一个版本

返回的是map<size_t, string>这个数据量很大,效率低下。

TextQuery.h

#inlucde<vector>
#include<map>
#include<set>
#include<iterator>
#include<fstream>
#include<iostream>
#include<sstream> using namespace std; #ifndef TEXTQUERY__H
#define TEXTQUERY__H class TextQuery {
public:
TextQuery() = default;
TextQuery(ifstream &is);
map<size_t, string> query(const string &str); private:
vector<string> text;
map<string, set<size_t>> lut;
}; #endif

TextQuery.cpp

include "TextQuery.h"

TextQuery::TextQuery(ifstream &is) {
string line;
istringstream iss;
string word;
size_t line_cnt = 0; while(getline(is, line)) {
iss.clear();
iss.str(line);
text.push_back(line);
while(iss>>word) {
if(lut.fine(word) != lut.end()) {
lut[word].insert(line_cnt);
} else {
lut[owrd] = set<size_t>{line_cnt};
}
}
++line_cnt;
}
} map<size_t, string> TextQuery::query(const string &str) {
map<size_t, string> sh;
auto p = lut.find(str);
if((p != lut.end()) {
for(auto iter = p->second.begin(); iter != p->second.end(); ++iter) {
sh[*iter] = text[*text];
}
} return sh;
}

这里用到了set<size_t>{line_cnt}来构造一个匿名的对象,赋值给map。但是不能用set<size_t> (line_cnt),因为set没有有参数的构造函数,有参数的也是拷贝的那种。

书上的版本

TextQuery.h

#pragma once
#include<vector>
#include<string>
#include<map>
#include<set>
#include<memory>
#include<fstream>
#include<sstream>
#include<iostream>
using namespace std; class QueryResult;
class TextQuery {
public:
TextQuery(ifstream &s);
QueryResult query(const string &str) const; private:
shared_ptr<vector<string>> text;
map < string, shared_ptr<set<size_t>>> lut;
}; class QueryResult {
public:
QueryResult(const string &str, shared_ptr<vector<string>> ptr, shared_ptr<set<size_t>> ln):sh(str), contents(ptr), lines(ln) {} shared_ptr<vector<string>> contents;
shared_ptr<set<size_t>> lines;
string sh;
}; void print(ostream &os, const QueryResult &qr);

TextQuery.cpp

#include "TextQuery.h"

TextQuery::TextQuery(ifstream &is) : text(make_shared<vector<string>>()) {
string line;
istringstream iss;
string word; while (getline(is, line)) {
iss.clear();
iss.str(line);
text->push_back(line);
//cout << line << endl;
while (iss >> word) {
//cout << word << endl;
auto &lines = lut[word];
if (!lines) {
lines.reset(new set<size_t>{ text->size() - 1 });
          // lines = make_shared<set<size_t>>();
         // lines->insert(text-size() -1);
} else {
lines->insert(text->size() - 1);
}
}
}
} QueryResult TextQuery::query(const string &str) const{
static shared_ptr<set<size_t>> st_ptr(new set<size_t>);
auto loc = lut.find(str);
if(loc != lut.end())
return QueryResult(str, text, loc->second);
else
return QueryResult(str, text, st_ptr); } void print(ostream &os, const QueryResult &pr) {
os << pr.sh << " occurs " << pr.lines->size() << " times"<<endl;
for (auto iter = pr.lines->begin(); iter != pr.lines->end(); ++iter) {
os << "(line " << *iter + 1 << ") " << (*pr.contents)[*iter] << endl;
}
}

测试代码

#include<iostream>
#include<fstream>
#include"TextQuery.h"
using namespace std; int main() {
ifstream is("TextQuery.cpp");
TextQuery tq(is); QueryResult qr = tq.query("QueryResult");
print(cout, qr); return 1;
}

这个代码相比我写的代码的优点

  1. 使用指针返回查找的数据,返回的数据量比较小,没有大量拷贝。
  2. 为了使用指针使用了shared_ptr
  3. 在判断map中lut[word]这个数据是否存在是巧妙的使用了map在没有word对应键的元素的时候会插入这个键,值使用值初始化,shared_ptr值初始化就是nullptr,根据是否为nullptr来确定是否需要分配set的空间。

c++ TextQuery程序的更多相关文章

  1. tuple类型的单词查询例子

    17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类. TextQuery.h #ifndef TEXTQUERY_H #define TEXTQUERY_H #in ...

  2. C++关联容器综合应用:TextQuery小程序

    本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer). 关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568sear ...

  3. Chapter12&Chapter13:程序实例

    文本查询程序 要求:程序允许用户在一个给定文件中查询单词.查询结果是单词在文件中出现的次数及所在行的列表.如果一个单词在一行中出现多次,此行只列出一次. 对要求的分析: 1.读入文件,必须记住单词出现 ...

  4. c++ primer( 文本查询程序)

    读取用户指定的任意文本文件,然后允许用户从该文件查找单词,查询的结果是该单词出现的次数,并列出每次出现所在的行,如果某单词在同一行中多次出现,程序将只显示改行的一次.行号按升序显示(int main( ...

  5. C++ Primer第四版 15.9 再谈文本查询 程序实现

    编程过程中发现书本中的示例程序并不完全,某些地方存在错误,现已改正并添加少许注释.. 1 #include<iostream> 2 #include<fstream> #inc ...

  6. C++ Primer 学习笔记_38_STL实践与分析(12)--集成的应用程序容器:文本查询程序

    STL实践与分析 --容器的综合应用:文本查询程序 引言: 本章中最重点的实例.由于不须要用到multiset与multimap的内容.于是将这一小节提到了前面.通过这个实例程序,大师分析问题的智慧, ...

  7. C++ 容器的综合应用的一个简单实例——文本查询程序

    C++ 容器的综合应用的一个简单实例——文本查询程序 [0. 需求] 最近在粗略学习<C++ Primer 4th>的容器内容,关联容器的章节末尾有个很不错的实例.通过实现一个简单的文本查 ...

  8. 《C++ Primer》 chapter 15 TextQuery

    <C++ Primer>中第15章为了讲解面向对象编程,举了一个例子:设计一个小程序,能够处理查询给定word在文件中所在行的任务,并且能够处理“非”查询,“或”查询,“与”查询.例如执行 ...

  9. C++ Primer 学习笔记_91_用于大型程序的工具 --命名空间

    用于大型程序的工具 --命名空间 引言: 在一个给定作用域中定义的每一个名字在该作用域中必须是唯一的,对庞大.复杂的应用程序而言,这个要求可能难以满足.这样的应用程序的全局作用域中一般有很多名字定义. ...

随机推荐

  1. 第10组 Beta冲刺 (1/5)

    1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/p/14012521.html ·作业博客:https://edu.cnblogs.co ...

  2. layui父表单获取子表单的值完成修改操作

    最近在做项目时,学着用layui开发后台管理系统. 但在做编辑表单时遇到了一个坑. 点击编辑时会出现一个弹窗. 我们需要从父表单传值给子表单.content是传值给子表单 layer.open({ t ...

  3. mysql 连接表 内连接 inner

    字段去重  关键字distinct 去除重复记录 可配合分组函数使用 select distinct job,deptno from emp; 未使用 distinct之前 使用后: 笛卡尔积现象:当 ...

  4. F2BPM的流程仿真

    仿真概述 F2BPM工作流仿真是一种通过建立工作流虚拟运行环境执行工作流仿真的方法.集中式仿真引擎解释工作流仿真模型,仿真活动的执行,处理仿真过程中的不确定性,从而完成工作流模型的仿真.同时,会实时显 ...

  5. 【reverse】逆向4 初识堆栈

    [reverse]逆向4 初识堆栈 1.问题引入 假设我们需要一块内存,有如下的要求 主要用于临时存储一些数据(如果数据很少可以放入寄存器中) 能够记录存了多少数据 能够非常快速的找到某个数据 2.模 ...

  6. C# app.config 保存和读取例子

    保存: Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); if ...

  7. vs python2.7 bug

    微软vs里面小细节的bug真他妈的多

  8. 关于启动bash提示‘bash: export: `//这是新的': not a valid identifier’的解决办法

    学习linux以来将centos改的也不少了,也不知道这个问题是由于那个修改来的.最近改bash的操作环境配置文件,用到了~/.bashrc这个文件,发现里面被我修改过. 那是当年安装fcitx输入法 ...

  9. gin框架中多种数据格式返回请求结果

    返回四种格式的数据:1. []byte.string  2. json格式  3. html模板渲染  4. 静态资源设置 package main import ( "github.com ...

  10. 腾讯云轻量服务器通过Docker搭建外网可访问连接的redis5.x集群

    总结记录/朱季谦 最近买了一台4核16的腾讯云轻量应用服务器,花了我快四百的大洋,打算搭建一堆docker组件集群,最先开始是通过docker搭建redis集群,计划使用三个端口,分别是7001,70 ...