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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759390557, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/C-C-plus-plus/CPP-Standard-Library/iomanip/setw(81): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78