/* 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.