Learning about manipulators

C++ streams have several different manipulators that may be used to control both input and output, some of which have already been discussed. The most common manipulator is std::endl, which outputs a newline and then flushes the output stream:

#include <iostream>int main(){    std::cout << "Hello World" << std::endl;}> g++ -std=c++17 scratchpad.cpp; ./a.outHello World

Another way to write this same logic is to use the std::flush manipulator:

#include <iostream>int main(){    std::cout << "Hello World\n" << std::flush;}> g++ -std=c++17 scratchpad.cpp; ./a.outHello World

Both are the same, although '\n' should always be used unless a flush is explicitly needed. For example, if multiple lines are needed, the following ...

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.