std::setw

suggest change
int val = 10;
// val is printed with no extra padding:
std::cout << val << std::endl;
 // val is right-aligned in a field of width 10. setw applies only to the next insertion:
std::cout << std::setw(10) << val << std::endl;

This outputs:

10
        10
1234567890

(where the last line is there to aid in seeing the character offsets).

Sometimes we need to set the width of the output field, usually when we need to get the output in some structured and proper layout. That can be done using std::setw of std::iomanip.

The syntax for std::setw is:

std::setw(int n)

where n is the minimum width of the next formatted output. A longer value is printed in full.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents