// 12_27.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<memory>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>
#include<map>
#include<set>
using namespace std; //提前声明类QueryResult,在书中说必须提前声明,然而vs2015中
//仅声明友元类亦可
class QueryResult; class TextQuery
{
//友元类的声明
friend class QueryResult;
public:
//构造函数
TextQuery(ifstream &is);
//返回查询的结果
QueryResult query(string s); private:
//用智能指针来保存数据,实现共享
shared_ptr<vector<string>> vecPtr;
shared_ptr<map<string, set<unsigned>>> mapPtr;
}; //TextQuery的构造函数
TextQuery::TextQuery(ifstream &is)
{
//用来暂存每一行的字符串
string word;
//记录行数
unsigned lineNum = ;
//对两个智能指针进行值初始化
vecPtr = shared_ptr<vector<string>>(new vector<string>());
mapPtr = shared_ptr<map<string, set<unsigned>>>(new map<string, set<unsigned>>());
while (getline(is, word))
{
//将每一行保存在类中
vecPtr->push_back(word);
istringstream iss(word);
string str;
while (iss >> str)
{
//将单词所在的行数记录下来
(*mapPtr)[str].insert(lineNum);
}
++lineNum;
}
} class QueryResult
{
public:
//QueryResult的构造函数,vecPtr实现共享
QueryResult(TextQuery &tq, string &s) :vecPtr(tq.vecPtr)
{
//mapPtr指针的值初始化
mapPtr = shared_ptr<map<string, set<unsigned>>>(new map<string, set<unsigned>>());
//将要查询的单词的结果保存下来
mapPtr->insert(pair<string, set<unsigned>>(s, (*tq.mapPtr)[s]));
}
//有缘函数,实现输出
friend ostream& print(ostream&,const QueryResult &qr);
private:
shared_ptr<vector<string>> vecPtr;
shared_ptr<map<string, set<unsigned>>> mapPtr;
}; //QueryResult的友元函数,实现输出
ostream& print(ostream &os,const QueryResult &qr)
{
//输出这个单词出现了多少次
os << (*qr.mapPtr).cbegin()->first << " occurs " << (*qr.mapPtr).cbegin()->second.size() <<
((*qr.mapPtr).cbegin()->second.size() > ? "times" : "time") << endl;
//输出单词所在的行数,和这一行的字符串
for (auto &e : qr.mapPtr->cbegin()->second)
os << "(line " << e << " ) " << (*qr.vecPtr)[e] << endl;
return os;
} //实现查询的函数
QueryResult TextQuery::query(string s)
{
return QueryResult(*this, s);
} void runQueries(ifstream &infile)
{
TextQuery tq(infile);
while (true)
{
cout << "enter word to look for,or q to quit: ";
string s;
if (!(cin >> s) || s == "q")break;
print(cout, tq.query(s)) << endl;
}
} int main()
{
ifstream ifs("D:\\file\\12_27.txt");
if (!ifs)
{
cerr << "open file failed!" << endl;
exit(-);
}
runQueries(ifs);
return ;
}

C++primer 练习12.27的更多相关文章

  1. 12.27 cf div3 解题报告

    12.27 cf div3 解题报告 wxy.wxy,带上分拉,全场做了个无脑小白 比赛场地 A: T1,跟着模拟就好了 B: sort一遍之后 去除的数一定是a[1]或者a[n] 比较去除谁小就输出 ...

  2. 武汉Uber优步司机奖励政策(12月21日-12.27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 2014 12 27 bestcoder 第一题

    水的不行不行的一道题 也是自己做的第一道题  纪念下 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h ...

  4. 厦门Uber优步司机奖励政策(12月21日-12.27日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. C++primer 练习12.6

    编写函数,返回一个动态分配的int的vector.将此vector传递给另一个函数,这个函数读取标准输入,将读入的值 保存在vector元素中.再将vector传递给另一个函数,打印读入的值.记得在恰 ...

  6. (原)Eclipse Tomcat配置(2014.12.27——By小赞)

    Eclipse中配置自己已经安装的Tomcat 首先为Eclipse安装Tomcat插件: 进入Tomcat插件下载页:http://www.eclipsetotale.com/tomcatPlugi ...

  7. Eclipse SVN插件安装与使用(2014.12.27——by小赞)

    安装参考:http://www.cnblogs.com/xdp-gacl/p/3497016.html 用法参考:http://blog.sina.com.cn/s/blog_8a3d83320100 ...

  8. C++ primer 练习 12.7

    重做上一题,这次使用shared_ptr 而不是内置指针.上一题题目为:(编写函数,返回一个动态分配的int的vector.将此vector传递给另一个函数,这个函数读取标准输入,将读入的值保存在ve ...

  9. ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98) [30-Jan-2018 16:12:27] ERROR: FPM initialization failed解决方法

    1.php启动之后发现访问nginx出现502错误,检查nginx.conf发现指定的php socket不存在 2.解决方法nginx修改陈这样,直接把绿色部门的socket写成本地地址+端口就可以 ...

随机推荐

  1. Spring注解实例

    public class ActivityAction extends CoreAction { private static final Logger log = Logger.getLogger( ...

  2. Amoeba:开源的分布式数据库Porxy解决方案

    http://www.biaodianfu.com/amoeba.html 什么是Amoeba? Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件 ...

  3. C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍

    一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...

  4. java指针

    import java.util.ArrayList; import java.util.List; public class TestPoint { public static void main( ...

  5. [Hibernate] - Query Select

    测试了常用的一些HQL查询方法,具体HQL的强大可以参考: http://docs.jboss.org/hibernate/orm/3.5/reference/zh-CN/html/queryhql. ...

  6. bzoj4642: 泡泡

    Description "OI真的像是一条奇趣横生的路啊,也许它是绕过了高考的大山,也许确实有通往大学的捷径.但我,真的,真的只在 乎那路上美丽的泡泡." --TB   TB喜欢所 ...

  7. Saltstack系列1:安装配置

    安装 安装EPEL作为安装Ansible的yum源(CentOS6.4) rpm -Uvh http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release ...

  8. TortoiseSVN使用详细步骤

    1 安装及下载client 端 2 什么是SVN(Subversion)? 3 为甚么要用SVN? 4 怎么样在Windows下面建立SVN Repository? 5 建立一个Working目录 6 ...

  9. 在mac上安装svn客户端

    mac 10.8上面,默认是没有svn客户端的,可以通过以下两种方式来安装:    1.单独安装xcode命令行工具,安装完成后,在终端中就可以使用svn命令了,目前版本是1.6.18:    2.去 ...

  10. Java事务处理全解析(二)——失败的案例

    在本系列的上一篇文章中,我们讲到了Java事务处理的基本问题,并且讲到了Service层和DAO层,在本篇文章中,我们将以BankService为例学习一个事务处理失败的案例. BankService ...