Reputation: 3164
On chapter 11 from C++ Primer 5 Edition, the title "The Word Transformation Program" it has a function called word_transform()
which is defined this way:
void word_transform(ifstream &map_file, ifstream &input)
{
auto trans_map = buildMap(map_file); // store the transformations
string text; // hold each line from the input
while (getline(input, text)) { // read a line of input
istringstream stream(text); // read each word
string word;
bool firstword = true; // controls whether a space is
printed
while (stream >> word) {
if (firstword)
firstword = false;
else
cout << " "; // print a space between words
// transform returns its first argument or its transformation
cout << transform(word, trans_map); // print the output
}
cout << endl; // done with this line of input
}
}
What I see if I am not wrong that the Boolean object firstword
is redundant here and there's no need to check for a space and instead simply after calling transform_word
which returns either a transformed text or original one then print it and simply print a space after it:
void word_transform(ifstream &map_file, ifstream &input)
{
auto trans_map = buildMap(map_file); // store the transformations
string text; // hold each line from the input
while (getline(input, text)) { // read a line of input
istringstream stream(text); // read each word
string word;
while (stream >> word)
cout << transform(word, trans_map) << " ";
cout << endl; // done with this line of input
}
}
As you can see the function works fine. Please help whether I am correct or use the book's function.
Upvotes: 1
Views: 115
Reputation: 27577
Sure, your way prints all the transform(word, trans_map)
separated by spaces. But you also print a redundant space at the end, which may or may not be of concern.
Upvotes: 2