C++关联容器综合应用:TextQuery小程序
2014-06-17 09:39
495 查看
本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer)。
关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568searchx/archive/2012/10/08/2715306.html
运行结果:
关于关联容器的概念及介绍,请参考园子里这篇博文: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=0;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==1)?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)+1<<") " <<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<2||!open_file(infile,argv[1])) { 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 0; }
运行结果:
相关文章推荐
- C++ Primer Chapter 10 关联容器之容器的综合应用:文本查询程序
- c++智能指针关联容器应用实例:文本查询程序
- C++ 容器的综合应用的一个简单实例——文本查询程序
- C++ 容器的综合应用的一个简单实例——文本查询程序
- C++ Primer 关联容器--容器的综合应用:文本查询程序
- C++ 容器 综合应用 文本查询程序
- c++ primer--容器的综合应用:文本查询程序
- 容器的综合应用:文本查询程序
- C++ 关联容器详解——从内部结构到应用
- 容器的综合应用--文本查询程序
- c++ 关联容器详解——从内部结构到应用
- C++ Primer 学习笔记_38_STL实践与分析(12)--容器的综合应用:文本查询程序
- 容器的综合应用:文本查询程序
- 容器的综合应用:文本查询程序(摘自C++ Primer)
- c++——容器实现文本查询程序
- 小词典 程序 C++ STL (容器部分)练习
- C++关联容器 Multiset and set
- Flex容器综合应用以及皮肤的添加效果 推荐
- C++ 关联容器 map
- C++-关联容器类型map