Hi, I am just beginning programming and I picked up the book Accelerated C++ because of all the recommendations and great reviews.
It's supposed to be a good beginner book but I am on chapter 3 and I am already beginning to struggle.
I got stuck on exercise 3-3 but was able to find the solution on Chapter 3 – Working with Batches of Data « Parks Computing
Unfortunately even while combing through the code and trying to review while retyping in codeblocks I am still confused and not quite understanding how the program worked successfully in the end.
The exercise asks you to write a program to count how many times each distinct word appears in its input.
This is the code provided on parkscomputing.com
I put an * on the pieces I have a question about.Code:#include <algorithm>#include <iostream>#include <string>#include <vector>using std::cin;using std::cout;using std::endl;using std::sort;using std::string;using std::vector;int main(){ // Ask for and read the words cout << "Please enter a few words, followed by end-of-file: "; vector<string> words; string word; // Invariant: words contains all of the words read so far while (cin >> word) words.push_back(word); *1 typedef vector<string>::size_type vec_sz; vec_sz size = words.size(); 1* // Check that the user entered some words if (size == 0) { cout << endl << "You didn't enter any words. " "Please try again." << endl; return 1; } *2 // sort the words sort(words.begin(), words.end()); 2* string current_word; int count; // Set the initial word to the first word in the vector current_word = words[0]; // Set the initial count for the first word count = 1; *3 // Invariant: we have counted current_index of the total words // in the vector for (vec_sz current_index = 1; current_index < size; ++current_index) { // Report the count for the current word if it does not match // the word at the current index in the vector, and reset the // count to zero so that it will one when the variable is // incremented outside the if statement. if (current_word != words[current_index]) { cout << "The word \"" << current_word << "\" appears " << count << " times." << endl; current_word = words[current_index]; count = 0; } *3 ++count; } *4 // Report the count for the final word cout << "The word \"" << current_word << "\" appears " << count << " times." << endl; 4* // We have reported the count of all the words in the vector, so exit. return 0;}
*1 - I understand what typedef does but I don't understand the piece of code it shortens. (vector<string>::size_type) Why exactly do I need to create vec_sz size = words.size()? And how is it used later in the program?
*2 - Is it possible to call these .begin() and .end() values for output or a statement or do I must first put them in something a type string or type int if it were a number?
*3 - is where I completely got lost. I really don't understand the logic behind all of this.
*4 - Why is this last part separate from *3? In all the other for/if/while implementations so far in the book we haven't needed to use a different piece of code for the last output.
Thanks for the help!