what are manipulators in c++
Manipulators in C++ are used to format the output of data that is displayed on the console. They are functions or objects that modify the stream buffer associated with the standard input/output streams. Here are some common manipulators in C++:
- setw(int width):
Sets the field width of the next input/output operation to the specified number of characters.
setprecision(int n):
Sets the precision of floating-point numbers to the specified number 'n'.
setfill(char ch):
Sets the fill character for the field width.
setiosflags(int flags):
Sets various format flags specified by the 'flags' parameter.
resetiosflags(int flags):
Resets the format flags specified by the 'flags' parameter.
setbase(int base):
Sets the basefield format flag for the stream.
setf(int flags):
Sets various format flags specified by the 'flags' parameter.
unsetf(int flags):
Unsets the format flags specified by the 'flags' parameter.
hex, oct, dec:
Sets the basefield to hexadecimal, octal, or decimal respectively.
boolalpha, noboolalpha:
- Controls the output for boolean values to be either textual (true/false) or numeric (1/0).
showpoint, noshowpoint:
- Shows or hides the decimal point and trailing zeros for floating-point values.
showpos, noshowpos:
- Shows or hides the positive sign for positive values.
uppercase, nouppercase:
- Converts alphabetic characters to uppercase or leaves them as they are.
skipws, noskipws:
- Enables or disables skipping leading whitespaces during input operations.
ws:
- Skips leading whitespaces during input operations.
These manipulators can be used in conjunction with the insertion (<<) and extraction (>>) operators to control the formatting of input and output streams.