C++关联容器综合应用:TextQuery小程序
本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer)。
关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568searchx/archive/2012/10/08/2715306.html
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<map>
#include<vector>
#include<set>
using namespace std;
//文本查询程序
class TextQuery
{
public:
typedef vector<string>::size_type line_no; void read_file(ifstream &is)
{
store_file(is);
build_map();
}
set<line_no> run_query(const string&) const;
string text_line(line_no) const;
private:
void store_file(ifstream&);
void build_map();
vector<string> lines_of_text;
map<string,set<line_no> > word_map;
};
void TextQuery::store_file(ifstream &is)
{
string textline;
while(getline(is,textline))
lines_of_text.push_back(textline);
}
void TextQuery::build_map()
{
//process each line
for(line_no line_num=;line_num!=lines_of_text.size();++line_num)
{
istringstream line(lines_of_text[line_num]);
string word;
while(line>>word)
//add thie line number to the set
//subscript will add word to the map if it's not already there
word_map[word].insert(line_num);
}
}
set<TextQuery::line_no> TextQuery::run_query(const string &query_word) const
{
//Note:must use find and not subscript the map directly
//to avoid adding words to word_map!
map<string,set<line_no> >::const_iterator loc=word_map.find(query_word);
if(loc==word_map.end())
return set<line_no>();//not found, return empty set.
else
return loc->second;
}
string TextQuery::text_line(line_no line) const
{
if(line<lines_of_text.size())
return lines_of_text[line];
throw out_of_range("line number out of range");
}
string make_plural(size_t ctr,const string &word,const string &ending)
{
return (ctr==)?word:word+ending;
}
void print_results(const set<TextQuery::line_no>& locs,const string& sought,const TextQuery& file)
{
typedef set<TextQuery::line_no> line_nums;
line_nums::size_type size=locs.size();
cout<<"\n"<<sought<<" occurs "
<<size<<" "
<<make_plural(size,"time","s")<<endl;
line_nums::const_iterator it=locs.begin();
for(;it!=locs.end();++it)
{
cout<<"\t(line "
<<(*it)+<<") "
<<file.text_line(*it)<<endl;
}
}
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear(); in.open(file.c_str());
return in;
}
//program takes single argument specifying the file to query
int main(int argc,char **argv)
{
ifstream infile;
if(argc<||!open_file(infile,argv[]))
{
cerr<<"No input file!"<<endl;
return EXIT_FAILURE;
}
TextQuery tq;
tq.read_file(infile);//build query map
//iterate with the user:prompt for a word to find and print results
//loop indefinitely;the loop exit is inside the while
while(true)
{
cout<<"enter word to look for, or q to quit:";
string s;
cin>>s;
if(!cin||s=="q") break;
set<TextQuery::line_no> locs=tq.run_query(s);
//print count and all occurrences, if any
print_results(locs,s,tq);
}
return ;
}
运行结果:
C++关联容器综合应用:TextQuery小程序的更多相关文章
- 如何玩转小程序+公众号?手把手教你JeeWx小程序CMS与公众号关联
随着微信小程序新功能.新入口的不断更新,小程序的商业价值逐步增强,特别是小程序与公众号的深度融合,已经让小程序成为各行业新的营销渠道.Jeewx平台专注小程序的开发,逐步完善小程序生态圈,通过简单操作 ...
- 微信小程序-视频教程-百度云-下载
链接: https://pan.baidu.com/s/16WGL3whutozx-UXqsDPhhA 提取码: 关注公众号[GitHubCN]回复获取 什么是微信小程序?小程序是一种不需要下载安 ...
- 某客的《微信小程序》从基础到实战视频教程
第 1 部分 微信小程序从基础到实战课程概要 第 1 节 微信小程序从基础到实战课程概要 1.1微信小程序从基础到实战课程概要 第 2 部分 初识微信小程序 第 1 节 微信小程序简 ...
- 微信小程序怎么用?线下商家最适合玩小程序
随着微信小程序不断地释放新功能,许多行业越来越关注小程序,目前已经有不少餐饮和线下传统零售企业开始谋划利用好小程序.但是,线下商业有着复杂的场景,如何针对自己行业的特点和需求开发出属于自己的小程序,是 ...
- 微信小程序初使心得【微信小程序快速入门】
摘要: 2016年推出微信小程序,时至今日,历经几个版本的更新,已形成了相对实用和稳定的服务平台.本文简单的介绍了微信小程序的入门用法,今后会继续关注和实践. 2016年推出微信小程序,时至今日,历经 ...
- 全栈开发工程师微信小程序-上(中)
全栈开发工程师微信小程序-上(中) width: 750rpx; 750rpx代表与屏幕等宽,rpx的缩写responsive pixel,这个单位是可以根据屏幕大小进行自适应调整的像素单位. 小程序 ...
- 小程序开发运营必看:微信小程序平台运营规范
一.原则及相关说明 微信最核心的价值,就是连接——提供一对一.一对多和多对多的连接方式,从而实现人与人.人与智能终端.人与社交化娱乐.人与硬件设备的连接,同时连接服务.资讯.商业. 微信团队一 ...
- 微信小程序推广方案
拥有小程序只是基础,能玩转小程序运营才是关键.本文将会简单讲述十种最实用的小程序推广策略,结合具体案例阐述商家企业如何在拥有小程序后玩转小程序,快速实现小程序的推广. 一. 公众号+小程序 小程序可以 ...
- 软件测试中的微信小程序怎么测试?
1.没有需求文档时,如何测试小程序?现在大多数公司的开发模式是:敏捷模式(用户故事) ,即以什么身份做什么事情会出现什么样的结果.那实际测试过程中,没有需求文档时,测试可以采用以下方式更好的完成测试工 ...
随机推荐
- 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注
原文 http://blog.csdn.net/esricd/article/details/7587136 在ArcGIS API for Silverlight/WPF中原版的TextSymbol ...
- 2014第一周五开发问题记URL传参乱码等
今天修改了页面中URL传中文参数乱码问题,本来远离通过在tomcat中配置URIEncoder是可以解决所有乱码问题的,但怕以后有人下载一个新的tomcat然后直接把程序放里面运行然后再发现乱码问题而 ...
- Sort Colors 解答
Question Given an array with n objects colored red, white or blue, sort them so that objects of the ...
- hdu 5610 Baby Ming and Weight lifting
Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...
- Selenium模块化
概述 高内聚低耦合是软件设计的一个基本原则. 内聚:从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事.它描述的是模块内的功能联系. 耦合:各模块之间相互连接的一种度量,耦合强弱取决于模块 ...
- Android 之 Gallery
1 在 xml 布局中添加 Gallery activity_main.xml <LinearLayout xmlns:android="http://schemas.andro ...
- windows下搭建及配置mantis缺陷管理工具
在windows XP 操作系统下,如何更快.更容易地搭建及配置mantis缺陷管理工具呢?以下是我实践的具体步骤: 一.安装mantis的前提环境,需要先安装Apache HTTP Server2. ...
- javascript 判断系统设备
<script> function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.pl ...
- RMAN-format变量及configuration配置项
一.FORMAT字符串替代变量使用FORMAT参数时可使用的各种替换变量,如下:%c:备份片的拷贝数(从1开始编号):%d:数据库名称:%D:位于该月中的天数 (DD):%M:位于该年中的月份 (MM ...
- android开发工具类总结(一)
一.日志工具类 Log.java public class L { private L() { /* 不可被实例化 */ throw new UnsupportedOperationException ...