Weekly C++ 15 – I/O Manipulators

In our programs, especially for small ones, to display some values ​​quickly, one of the most common methods we use is to print something on standard output.
For C ++, the most common method used for this is probably std :: cout. So how much are we familiar with this library’s abilities? In this article, I will bring you the associated features to your attention.
The structures that allow you to control what you print on the standard output are called “I/O manipulators”. To use them, you need to include the header file “#include <iomanip>” in your code. Most of these manipulators are actually a function and are specially designed to use with <<, >>, operators, but you can also call it as a function (as some of my readers have noticed, setw, setprecision, setbase, setfill, resetiosflags, setiosflags are not used this way). Let’s look at an example:

give the same output.

Now, let us look at important manipulators. As in my other writings, I will try to focus on the most important ones, one by one. For a complete list, refer to the pages in the resources section.

  • boolalpha, noboolalpha:
    • Used for textual / numerical representation of boolean values. The default numeric (noboolalpha) notation is active, true values are displayed as 1 and false values are displayed as 0. With boolalpha, these values are displayed as “true” and “false“, respectively.
  • showbase, noshowbase:
    • In the case where showbase API is called, the numbers are displayed according to the 8 numbering base system are shown with 0, the ones according to the 16 basis are shown as 0x.
  • showpoint, noshowpoint:
    • When showpoint is used, decimal parts are always shown. By default, it is rounded to an integer.

  • showpos, noshowpos:
    • With showpos, positive numbers are displayed with “+”. Not displayed by default.
  • hexfloat, fixed scientific, defaultfloat:
    • As you can tell from their names, these functions determine how decimal numbers are displayed. It also extracts the values given as input accordingly. I think it would be better to give an example about their usage.
  • setprecision(n):
    • When a decimal number is displayed, it determines the number of digits to be displayed after the period. The default is 6.
  • setbase(n):
    • Specifies the numbering base system used for input and output which can be either 8, 10, 16 and 0. Although 0 is a decimal notation for output, it tells system to determine numbering base system automatically with respect to entered number for the input. You can get the same result with setbase (10), setbase (16) and setbase (8).
  • ws:
    • Removes the space characters until it sees the first non-space character.

  • ends:
    • Adds ‘\ 0’ to the output.
  • flush:
    • Sends the output stacked characters to print.
  • endl:
    • Adds ‘\ n’ to the output and sends it to print.
  • setiosflags(n)/resetiosflags(n):
    • Assigns or resets the given std::ios values.

  • setw(n):
    • Determines the width defined for the next output. If there is any space left in this field, it is filled with the character specified by setfill.
  • setfill(c):
    • Specifies the character to be used to fill in the blanks. The spaces herein refer to spaces that are displayed when a character less than the width defined by setw is displayed. The space character is used by default.
  • internal, left, right:
    • Specifies where to place the characters specified for the space. The left and right option can be used for each output, while internal is only used for integer, decimal, and monetary representations.

       

Yes my friends, now on you can use the console, standard output, in your applications as you like 🙂

Hope to meet you in my next article.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.