function
<iomanip>

std::setw

/*undefined*/ setw (int n);
Set field width
Sets the field width to be used on output operations.

Behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).

This manipulator is declared in header <iomanip>.

Parameters

n
Number of characters to be used as field width.

Return Value

Unspecified. This function should only be used as a stream manipulator (see example).

Example

1
2
3
4
5
6
7
8
9
// setw example
#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw

int main () {
  std::cout << std::setw(10);
  std::cout << 77 << std::endl;
  return 0;
}


Output:
        77

Data races

The stream object on which it is inserted/extracted is modified.
Concurrent access to the same stream object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream is in a valid state.

See also