C++ manipulators

Definition:
Manipulators are helping functions that can modify the input/output stream. It does not mean that we change the value of a variable, it only modifies the I/O stream using insertion (<<) and extraction (>>) operators.

Types of C++ Manipulators


Stream Manipulators:



    A stream manipulator is a function that can be inserted into a stream with the << or >> operator, just like any other data type. For example, std::endl is a stream manipulator that inserts a newline character and flushes the stream. 

    • setw(int width): Sets the field width for the next input or output operation.
    • setprecision(int n): Sets the number of digits to be displayed after the decimal point for floating-point numbers.
    • setfill(char c): Sets the fill character used to pad the output.
    • left: Sets the output to be left-justified.
    • right: Sets the output to be right-justified.




    Function Manipulators:



    Function manipulators in C++ are special functions that temporarily modify the behavior of the input/output streams. They are used with the insertion (<<) or extraction (>>) operators to control formatting, such as setting precision for floating-point numbers or changing the display base for integers.

    • endl: Inserts a newline character into the output stream and flushes the stream.
    • flush: Flushes the output buffer, ensuring that all data is written to the output device.
    • hex, oct, dec: Changes the base in which integers are displayed (hexadecimal, octal, decimal).
    • fixed: Sets the floating-point output format to fixed-point notation.
    • scientific: Sets the floating-point output format to scientific notation.















































    Comments

    Popular posts from this blog