C++ primer has the following description and example (p. 110):
assume we have a vector<string> named text that holds the
data from a text file. Each element in the vector is either a sentence or an empty
string representing a paragraph break. If we want to print the contents of the first
paragraph from text, we’d write a loop that iterates through text until we
encounter an element that is empty:
for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
cout << *it << endl;
What I don't understand is how can a newline character \n
be interpreted as an empty vector when reading from a text file or even user input?
I set up a loop to read input into a vector as follows, which is the method described in the book:
vector<string> text;
string s;
while (cin >> s)
text.push_back(s);
Receiving inputs in this manner erases white space including newlines does it not? How would I interpret newlines and store them as empty vectors? Why does the book describe it in this manner?
The book describes input re-direction in the earlier pages by doing the following in CMD in Windows:
cd [PROJECT LOCATION]
PROJECT.EXE <input.txt>output.txt
The above is how I would read input from a file into my program.