本文介绍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. Why Functional Programming Matters

    http://hi.baidu.com/lhurricane/item/35b57e12a1e3c5ddbf9042a7 http://blog.csdn.net/ddwn/article/detai ...

  2. 【转】 linux内核移植和网卡驱动(二)

    原文网址:http://blog.chinaunix.net/uid-29589379-id-4708911.html 一,内核移植步骤: 1, 修改顶层目录下的Makefile ARCH       ...

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

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

  4. hdu 3478 Catch(染色 dfs 或 bfs )

    Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...

  5. poj 3370 Halloween treats(鸽巢原理)

    Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...

  6. 也谈---基于 HTTP 长连接的“服务(转载)

    这里指讨论基于HTTP的推技术, 诸如flash,applet之类的东西不作分析, 他们就不能说是"纯粹"的浏览器应用了. 首先是一点背景知识, 大家都知道长连接避免了tcp连接的 ...

  7. Spring(三)——AOP

    AOP全名为Aspect-Oriented Programming,意思是面向横切面编程,前边我们有过介绍   面向横切面编程AOP的理解 ,我们通过这种编程思想很容易的扩展我们的应用程序. 一,如何 ...

  8. Alexandra and Prime Numbers(思维)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. POJ3071:Football(概率DP)

    Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...

  10. .NET 基础串讲

    C#基础 .NET介绍 —计算机发展史 第一代语言:机器语言 0101 第二代语言:汇编语言, 用一些简洁的英文字母.符号串来替代一个特定指令的二进制串 第三代语言:接近于数学语言或人的自然语言,同时 ...