C++ iomanip setw() Function
Example
Print values in a field that is 6 characters wide:
cout << "[" << setw(6) << 42 << "]\n";
cout << "[" << setw(6) << "Hi" << "]";
Definition and Usage
The setw() function sets the minimum width of the next value that is printed. If the value is shorter, spaces are added in front of it.
This is useful when you want to print values in neat columns, like in a table.
The setw function is defined in the <iomanip> header file.
Note: Unlike most manipulators, setw() only works for the next value. Use left to add the spaces after the value instead, and setfill() to use another character than a space.
Syntax
setw(int n)
Parameter Values
| Parameter | Description |
|---|---|
| n | Required. The minimum number of characters to use for the next value. |
Technical Details
| Returns: | A manipulator that you use with <<. It changes the width of the next value only. |
|---|