What are common stream manipulators?

0210

Common stream manipulators in C++ are functions or objects that modify the behavior of input and output streams. Here are some of the most frequently used stream manipulators:

  1. std::endl: Inserts a newline character and flushes the output buffer.

    std::cout << "Hello, World!" << std::endl;
  2. std::flush: Flushes the output buffer without inserting a newline.

    std::cout << "Flushing output" << std::flush;
  3. std::setw(int n): Sets the width of the next output field to n characters.

    std::cout << std::setw(10) << 42; // Outputs "        42"
  4. std::setprecision(int n): Sets the decimal precision for floating-point output.

    std::cout << std::setprecision(3) << 3.14159; // Outputs "3.14"
  5. std::fixed: Displays floating-point numbers in fixed-point notation.

    std::cout << std::fixed << 3.14159; // Outputs "3.141590"
  6. std::scientific: Displays floating-point numbers in scientific notation.

    std::cout << std::scientific << 3.14159; // Outputs "3.141590e+00"
  7. std::boolalpha: Outputs boolean values as true or false instead of 1 or 0.

    std::cout << std::boolalpha << true; // Outputs "true"
  8. std::noboolalpha: Outputs boolean values as 1 or 0 instead of true or false.

    std::cout << std::noboolalpha << true; // Outputs "1"
  9. std::hex: Changes the base of integer output to hexadecimal.

    std::cout << std::hex << 255; // Outputs "ff"
  10. std::dec: Changes the base of integer output to decimal.

    std::cout << std::dec << 255; // Outputs "255"
  11. std::oct: Changes the base of integer output to octal.

    std::cout << std::oct << 255; // Outputs "377"

Summary:

These manipulators help control the formatting of output in C++, making it easier to present data in a readable and structured manner. They can be combined and used in various ways to achieve the desired output format.

0 Comments

no data
Be the first to share your comment!