setw


manipulator function
<iomanip>
smanip setw ( int n );

Set field width

Sets the number of characters to be used as the field width for the next insertion operation.

Behaves as if a call to the stream's member ios_base::width with n as its argument was made.

The field width determines the minimum number of characters to be written in some output representations. If the standard width of the representation is shorter than the field width, the representation is padded with fill characters (see setfill) at a point determined by the format flag adjustfield (left, right or internal).

This manipulator is declared in header <iomanip>, along with the other parameterized manipulators: resetiosflags, setiosflags, setbase, setfill and setprecision. This header file declares the implementation-specific smanip type, plus any additional operator overload function needed to allow these manipulators to be inserted and extracted to/from streams with their parameters.

Parameters

n
Number of characters to be used as field width.


Return Value

Unspecified. This function should only be used as a stream manipulator.

Example

1
2
3
4
5
6
7
8
9
10
// setw example
#include <iostream>
#include <iomanip>
using namespace std;

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


This code uses setw to set the field width to 10 characters.

See also