// 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. JavaScript理解

    Javascript,是从类型(type)开始,这些类型在JS中分为两大类:原生类型与对象类型.原生类型包括:number,string, boolean, null, undefined:剩下的非原 ...

  2. JAVA线程池ThreadPoolExecutor-转

    首先是构造函数签名如下: public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeU ...

  3. memcached windowns 安装使用

    到csdn下载安装包吧,要不找我要,1033536868 安装: memcached -d install memcached -d start net start "Memcached S ...

  4. Java compiler level does not match the version of the installed Java project facet.问题

    从同事那里拷贝过来的web项目,导入到eclipse中,出现Java compiler level does not match the version of the installed Java p ...

  5. Spark(二): 内存管理

    Spark 作为一个以擅长内存计算为优势的计算引擎,内存管理方案是其非常重要的模块: Spark的内存可以大体归为两类:execution和storage,前者包括shuffles.joins.sor ...

  6. IOS的沙盒机制

    ios的沙盒(bandbox)机制:一种安全体系,ios应用程序只能对自己创建的应用程序进行读取文件,这个独立.封闭.安全的空间,就我们说的沙盒.它里面一般存放着你的程序需要的文件,数据持久化的一些文 ...

  7. jquery form表单序列号

    1.serialize()方法 格式:var data = $("form").serialize(); 功能:将表单内容序列化成一个字符串. 这样在ajax提交表单数据时,就不用 ...

  8. 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. Exception error message with incorrect line number

    In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset ...

  10. XAMPP Error: Apache shutdown unexpectedly. 解决思路

    我建议首先 运行在cmd中运行 (安装目录)apache/bin/httpd.exe 之后就很好确定错误的具体原因了,而不是根据下面的那样猜端口,比如我遇到的问题,就是配置的路径不存在导致的. 参考资 ...