What are the manipulator in c plus plus?

1 answer

Answer

1002471

2026-08-12 05:25

+ Follow

What is a manipulator?

C++ manipulators are functions or operators that are used specifically with input and output streams in order to manipulate those streams or to insert or extract special characters. Many manipulators apply to both input and output streams but some are specific to one or the other.

The following list briefly describes all the manipulators available in C++, including those only available in C++11. Most manipulators can be found in the <iOS> header, while the parameterised manipulators (those that accept arguments) will be found in the header.

Output stream manipulators <iOS>

std::flush - synchronises the output buffer with the controlled output sequence

std::endl - inserts a newline character (\n) and flushes the output stream

std::ends - inserts a null character (\0) without flushing the output stream

Input stream manipulators <iOS>

std::ws - extracts and discards whitespace characters from the input stream

Numerical base format manipulators ("basefield" flags) <iOS>

std::dec - inserts/extracts integral numeric data with decimal notation

std::hex - inserts/extracts integral numeric data with hexadecimal notation

std::oct - inserts/extracts integral numeric data with octal notation

Floating-point format manipulators ("floatfield" flags) <iOS>

std::fixed - inserts/extracts floating point values with fixed notation

std::scientific - inserts/extracts floating point values with scientific notation

std::hexfloat - inserts/extracts floating point values with hecadecimal notation (C++11 only)

std::defaultfloat - default behaviour (C++11 only)

Adjustment manipulators ("adjustfield" flags) <iOS>

std::internal - output is padded to the field width by inserting fill characters at a specified internal point

std::left - output is padded to the filed width by appending fill characters

std::right - output is padded to the field width by prepending fill characters

Flags (on) <iOS>

std::boolalpha - generates text values "true" and "false" for boolean values

std::showbase - generates the base for basefield values

std::showpoint - generates a decimal point for "floatfield" values

std::showpos - generates positive sign to positive values

std::skipws - ignores whitespace characters

std::unitbuf - flushes the buffer after every insertion

std::uppercase - generates upper-case letters for generated letters

Flags (off) <iOS>

std::noboolalpha - does not generate text values for boolean values

std::noshowbase - does not generate the base for basefield values

std::noshowpoint - does not generate decimal point for "floatfield" values unless the decimal portion is non-zero

std::noshowpos - does not generate positive sign for positive values

std::noskipws - does not ignore whitespace characters

std::nounitbuf - does not flush the buffer afer every insertion

std::nouppercase - does not generate upper-case letters for generated letters

Parameterised manipulators

std::setiOSflags - set format flags

std::resetiOSflags - reset format flags

std::setbase - set basefield flag

std::setfill - set fill character

std::setprecision - set floatfield precision

std::setw - set field width

Many of these manipulators work together. For instance, the std::internal, std::left and std::rightmanipulators all work in conjunction with the std::setwand std::setfill manipulators.

All manipulators are implemented as operators which can be chained together using the insertion (<<) or extraction (>>) operators as appropriate to the stream. Excluding the parameterised manipulators, most manipulators are also implemented as functions (passing the streram as an argument). Others, including all parameterised manipulators, are implemented as members of the stream. Some, such as std::flush, are implemented all three ways; as an operator, a function and a member function.

Some examples of manipulator usage:

// set field width to 8 characters for console output

std::cout << std::setw (8); // operator

std::cout.width (8); // member method

// set precision to 16 places for floatfields

std::cout << std::setprecision (16); // operator

std::cout.precision (16); // member method

// insert newline and flush stream

std::cout << std::endl; // operator

std::endl (std::cout); // function

// flush stream

std::cout << std::flush; // operator

std::flush (std::cout); // function

std::cout.flush (); // member method

// generate hexadecimal values with base prefix

std::cout << std::showbase << std::hex; // operator

std::showbase (std::cout); // function

std::hex (std::cout); // function

More information on manipulators can be found in the sources and related links section, below.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.