Search notes:

C++ Standard Library: std::setw

std::setw is a manipulator that sets the width of the next element that is printed to a stream object with the << operator. It is very useful to create tabular (aligned) output of data.
In the next simple example, the texts (txt) are printed with a width of 19 characters while the numbers (num) are printed with a width of 4 characters.
Because numbers are typically right aligned, the manipulator std::right is also used.
//
// g++ setw.cpp
//
#include <iostream>
#include <iomanip>

void printLine(std::string const& txt, int num) {
   std::cout  <<
   std::left  << std::setw(19) << txt <<
   std::right << std::setw( 4) << num <<
   std::endl;
}

int main() {

   printLine("forty-two"           ,  42);
   printLine("ninty-nine"          ,  99);
   printLine("one hundred and one" , 101);
   printLine("seven"               ,   7);
   printLine("fourteen"            ,  14);
   printLine("fünf"                ,   5);

}
Github repository about-cpp-standard-library, path: /iomanip/setw.cpp
Output:
forty-two            42
ninty-nine           99
one hundred and one 101
seven                 7
fourteen             14
fünf                 5
Note the problem with the umlaut ü.

See also

C++ Standard Library

Index