Comprehensive application of C++primer container: Text Query procedure

Source: Internet
Author: User

1#include <iostream>2#include <string>3#include <fstream>4#include <sstream>5#include <vector>6#include <map>7#include <Set>8 9 using namespace:: STD;Ten /*to complete a simple word Query task, you first need to create a class textquery to help us with this task, in which the private One internally called objects and functions, create read_file in public to read a file into a map container saved in a vector and associated with each word and line number A finds a string through the Run_query function and returns a set object containing each line number that the word appears to use Text_line to output each row*/ - classtextquery{ -  Public: thetypedef vector<string>:: Size_type line_no; -      voidRead_file (Ifstream & is)//each time the function reads a line from the file and saves it in the container vector -          //, the map container for each word and line number associated will be created when you are finished typing -{Store_file ( is); Build_map (); } +      Set<line_no> Run_query (Const string&)Const;//The parameter is a string object that returns a set object that contains the line number of all the rows of the string object -      stringText_line (Line_no)Const;//its formal parameter is a line number that returns the line of text that corresponds to the line number in the input text +  A  at Private: -     voidStore_file (Ifstream & is); -     voidBuild_map (); -vector<string>Lines_of_text; -map<string,Set<line_no> >Word_map; - }; in  - /*reads the contents of each line of the previously entered infile.txt into a Vector<string> object, including.*/ to voidTextquery::store_file (Ifstream & is) + { -     stringTextline; the      while(Getline ( is, textline)) *Lines_of_text.push_back (Textline);//Store has access to private objects because it is in public $     //cout<<lines_of_text[0];Panax Notoginseng  - } the /*use Istringstream to enter text in each row into a map container, Word_map[word] represents the Set<line_no> object + The set's Insert function is then called to add the current line number to the set object. If a word appears repeatedly in the same line, no action is made*/ A voidTextquery::build_map () the { +      for(Line_no line_num=0; Line_num!=lines_of_text.size (); + +line_num) -     { $ Istringstream Line (Lines_of_text[line_num]); $         stringWord; -      while(line>>word) - Word_map[word].insert (line_num); the  -     }Wuyi } the /*The run_query function has a reference parameter to the Const string object and accesses the Word_map object with this parameter as the subscript.  - Returns a corresponding set (which contains all occurrences of the line number) if it is found, or returns an empty set*/ Wu Set<TextQuery::line_no> Textquery::run_query (Const string&query_word)Const - { Aboutmap<string,Set<line_no>>:: Const_iterator $Loc=Word_map.find (Query_word); -     if(loc==word_map.end ()) -         return Set<line_no>(); -     Else A         returnLoc->second; +  the } - /*After you run Run_query, you get a set of line numbers for the words that are found. In addition to outputting the number of occurrences of the word, $ you also want to output each line where the word is located. is through the previous vector<string> to output*/ the stringTextquery::text_line (Line_no line)Const the { the     if(line<lines_of_text.size ()) the         returnLines_of_text[line]; -     ThrowOut_of_range ("Line number out of range"); in } the /*used to read a file*/ theIfstream &open_file (Ifstream &inch,Const string&file) About { the     inch. Close ();//Close in case it is already open the     inch. Clear ();//clear any exiting errors; the     inch. Open (File.c_str ());//Open the file we were given +     return inch; - } the /*if the number of outputs is greater than 1, then the output plural*/Bayi stringMake_plural (size_t Ctr,Const string&word,Const string&ending) the { the     return(ctr==1)? word:word+ending; - } - /*outputs the number of occurrences, calls the Make_plural function, and then outputs each occurrence of the line number, and calls Text_line to output each row*/ the voidPrint_result (Const Set<textquery::line_no>&locs,Const string&sought,ConstTextquery &file) the { thetypedefSet<TextQuery::line_no>line_nums; theLine_nums::size_type size=locs.size (); -cout<<"\ n"<<sought<<"occurs"<<size<<" " the<<make_plural (Size," Time","s") <<Endl; theLine_nums::const_iterator it=Locs.begin (); the      for(; It!=locs.end (); + +it)94     { thecout<<"\ t ( Line" the<< (*it) +1<<")" the<<file.text_line (*it) <<Endl;98     } About  - 101 }102 103 104 /*Create a Textquery object to implement a simple user query session. If a query is required, then call the Run_query function and return the word the The collection of rows that appear, and calls the Print_result function to output the function*/106 intMainintargcChar**argv)107 {108 ifstream infile;109     if(!open_file (infile,"Infile.txt")) the     ThrowRuntime_error ("no Remove file");111 textquery TQ; the Tq.read_file (infile);113      while(true) the     { thecout<<"Enter word to look for, or Q to quit:"; the         strings;117Cin>>s;118         if(!cin| | s=="Q") Break;119         Set<TextQuery::line_no> locs=Tq.run_query (s); - Print_result (LOCS,S,TQ);121     122     }123     return 0;124}
get away withSome "what ' common'

Comprehensive application of C++primer container: Text Query procedure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.