Reading a line or word at a time
Reading from standard input one line at a time is a very common task in simple scripts, and most scripting languages make it a one-liner. For example, in Python:
for line in sys.stdin: # preserves trailing newlines process(line)
And in Perl:
while (<>) { # preserves trailing newlines process($_); }
In C++, the task is almost as easy. Notice that C++'s std::getline
function, unlike the other languages' idioms, removes the trailing newline (if any) from each line it reads:
std::string line; while (std::getline(std::cin, line)) { // automatically chomps trailing newlines process(line); }
In each of these cases, the entire input never lives in memory at once; we are indeed "streaming" the lines through our program in an efficient manner. (And the std::getline
function is allocator-aware; if we absolutely need to avoid heap allocation, we can exchange std::string line
for std::pmr::string
.) The process
...