本文介绍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小程序的更多相关文章

  1. 如何玩转小程序+公众号?手把手教你JeeWx小程序CMS与公众号关联

    随着微信小程序新功能.新入口的不断更新,小程序的商业价值逐步增强,特别是小程序与公众号的深度融合,已经让小程序成为各行业新的营销渠道.Jeewx平台专注小程序的开发,逐步完善小程序生态圈,通过简单操作 ...

  2. 微信小程序-视频教程-百度云-下载

    链接: https://pan.baidu.com/s/16WGL3whutozx-UXqsDPhhA 提取码: 关注公众号[GitHubCN]回复获取   什么是微信小程序?小程序是一种不需要下载安 ...

  3. 某客的《微信小程序》从基础到实战视频教程

    第 1 部分 微信小程序从基础到实战课程概要   第 1 节 微信小程序从基础到实战课程概要   1.1微信小程序从基础到实战课程概要   第 2 部分 初识微信小程序    第 1 节 微信小程序简 ...

  4. 微信小程序怎么用?线下商家最适合玩小程序

    随着微信小程序不断地释放新功能,许多行业越来越关注小程序,目前已经有不少餐饮和线下传统零售企业开始谋划利用好小程序.但是,线下商业有着复杂的场景,如何针对自己行业的特点和需求开发出属于自己的小程序,是 ...

  5. 微信小程序初使心得【微信小程序快速入门】

    摘要: 2016年推出微信小程序,时至今日,历经几个版本的更新,已形成了相对实用和稳定的服务平台.本文简单的介绍了微信小程序的入门用法,今后会继续关注和实践. 2016年推出微信小程序,时至今日,历经 ...

  6. 全栈开发工程师微信小程序-上(中)

    全栈开发工程师微信小程序-上(中) width: 750rpx; 750rpx代表与屏幕等宽,rpx的缩写responsive pixel,这个单位是可以根据屏幕大小进行自适应调整的像素单位. 小程序 ...

  7. 小程序开发运营必看:微信小程序平台运营规范

    一.原则及相关说明 ​ 微信最核心的价值,就是连接——提供一对一.一对多和多对多的连接方式,从而实现人与人.人与智能终端.人与社交化娱乐.人与硬件设备的连接,同时连接服务.资讯.商业. ​ 微信团队一 ...

  8. 微信小程序推广方案

    拥有小程序只是基础,能玩转小程序运营才是关键.本文将会简单讲述十种最实用的小程序推广策略,结合具体案例阐述商家企业如何在拥有小程序后玩转小程序,快速实现小程序的推广. 一. 公众号+小程序 小程序可以 ...

  9. 软件测试中的微信小程序怎么测试?

    1.没有需求文档时,如何测试小程序?现在大多数公司的开发模式是:敏捷模式(用户故事) ,即以什么身份做什么事情会出现什么样的结果.那实际测试过程中,没有需求文档时,测试可以采用以下方式更好的完成测试工 ...

随机推荐

  1. Linux下ld搜索问题:ld: cannot find -l"XX"

    ld命令行工具(链接库的一个工具)的搜索路径是-L指定的,库名是-l指定的. 比如: ld -L[dir] -l[mylib] --verbose 以上我用可视化的方法显示ld的搜索路径,其结果是居然 ...

  2. mosquitto在Linux环境下的部署/安装/使用/测试

    mosquitto在Linux环境下的部署 看了有三四天的的源码,(当然没怎么好好看了),突然发现对mosquitto的源码有了一点点感觉,于是在第五天决定在Linux环境下部署mosquitto. ...

  3. 【转】理解 Android Build 系统----不错

    $ mmm -help用法:make [选项] [目标] ...选项: -b, -m 忽略兼容性. -B, --always-make Unconditionally make all targets ...

  4. 几个js的linq实现

    几个js的linq实现 linqjs.codeplex.com jslinq.codeplex.com javascriptiqueryable.codeplex.com fromjs.codeple ...

  5. 【转】使用 udev 高效、动态地管理 Linux 设备文件

    简介: 本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本 ...

  6. 【转】基于V4L2的视频驱动开发

    编写基于V4L2视频驱动主要涉及到以下几个知识点:1> 摄像头方面的知识 要了解选用的摄像头的特性,包括访问控制方法.各种参数的配置方法.信号输出类型等.2> Camera解码器.控制器 ...

  7. ffmpeg常用参数一览

    基本选项: -formats 输出所有可用格式 -f fmt 指定格式(音频或视频格式) -i filename 指定输入文件名,在linux下当然也能指定:0.0(屏幕录制)或摄像头 -y 覆盖已有 ...

  8. T4 模板 vs2010

    参阅:http://dotnet.cnblogs.com/page/78398/ T4模板的定义非常简单,整个模板的内容包括两种形式:静态形式和动态动态.前者就是直接写在模板中作为原样输出的文本,后者 ...

  9. Model注解的后台原理

    Asp.net MVC的验证特性是由模型绑定器.模型元数据.模型验证器和模型状态组成的协调系统的一部分. 1.验证和模型绑定 默认情况下,Asp.net MVC框架在模型绑定石执行验证逻辑,在操作方法 ...

  10. PHP学习笔记十二【数组排序】

    <?php $arr=array(0,5,-1); $temp=0; for($i=0;$i<count($arr)-1;$i++) { for($j=0;$j<count($arr ...