C ++ primer Chinese Version 4th exercise 10.9

Source: Internet
Author: User

/* Here I will write down some of my answers and comments for the exercises in C ++ primer 4th (Note: I didn't buy an answer book, so it is not guaranteed that it is correct. What do you think is wrong, I hope you can tell me) the requirements for source code running are the same as those in the book. The pre-compilation and using lines are omitted. If you still don't know what to say, you can ask me, but I am also a beginner and may not know, I know that I am a beginner when I am still studying C ++ Prime. You are welcome to repost it, but please keep the author's name "jiutian Yuling ". */

// A word query program, even if it is not a question, the actual code also has a certain significance in use

Int main (INT argc, char * argv []) // agrv [1] indicates the word to be queried, and agrv [2] indicates the query file
{
If (argc! = 3) // check the number of parameters
{
Cerr <"error: Wrong argment number. First was the word, second the file name .";
Return-1;
}
Ifstream ifile;
Ifile. Open (argv [2]);
If (! Ifile) // check the file opening status
{
Cerr <"error: Unable to open input file:" <argv [2] <Endl;
Return-1;
}
String line, word;
Map <string, int> wordcount;
While (Getline (ifile, line ))
{
Istringstream isstream (line );
While (isstream> word) // read each word
{
++ Wordcount [word];
// The form of t10.12 is to comment out the above sentence and remove the comment below. Of course, this is the first convenience.
/* Pair <Map <string, int >:: iterator, bool> ret =
Wordcount. insert (make_pair (word, 1 ));
If (! Ret. Second)
+ + Ret. First-> second ;*/
}
}
Ifile. Close ();
Cout <"the" <argv [1] <"occor" <wordcount [argv [1]
<(Wordcount [argv [1]> 1 )? "Times": "Time"); // output, with the last? : Operation to output the correct plural form
Return 0;
}
// The final problem with this program is that the word statistics use the common methods provided in the C ++ primer book, which are completely distinguished by space.
// There is a way to distinguish between numbers, punctuation, and other influences on words. For the time being, we cannot think of a simple method. The ideas are complicated and we need to deal with all kinds
// The situation. For example, the punctuation check may cause problems with abbreviations such as it's. If you have any idea about the method, please leave a message to me. :) as far as I know
// A textquery Program (10.6.3) provided in C ++ primer also has the same problem.

 

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.