O'Reilly logo

C++17 STL Cookbook by Jacek Galowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

In this section, we will print numbers with wildly varying format settings, in order to get familiar with I/O manipulators:

  1. First, we include all the necessary headers and declare that we use the std namespace by default:
      #include <iostream>      #include <iomanip>      #include <locale>            using namespace std;
  1. Next, we define a helper function that prints a single integer value with different styles. It accepts a padding width and a filling character for padding, which is set to space ' ' by default:
      void print_aligned_demo(int val,                               size_t width,                               char fill_char = ' ')      {
  1. With setw, we can set the minimum number of characters output for printing a number. If we print 123 with a width of 6, for example, we get " 123", or "123 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required