Advertisement
Advanced C++, I/O Streams

What are stream manipulators and how are they used?

Short answer

Manipulators are objects/functions that change stream formatting: std::endl (newline + flush), std::setw/std::setprecision/std::fixed (from ), std::hex/std::dec, std::boolalpha, and std::flush.

Save C++ mock interview
Advertisement
AModel answer
Manipulators are objects/functions that change stream formatting: std::endl (newline + flush), std::setw/std::setprecision/std::fixed (from <iomanip>), std::hex/std::dec, std::boolalpha, and std::flush. They're inserted into the stream to control width, precision, base, and formatting of subsequent output.
std::cout << std::fixed << std::setprecision(2) << 3.14159; // 3.14
Real-world example
A report formats currency with std::fixed and setprecision(2) for consistent decimal places.
Likely follow-ups
  • What does std::endl do besides newline? | Where do setw/setprecision come from?
Related topics