public member function
<ios> <iostream>

std::ios_base::width

get (1)
streamsize width() const;
set (2)
streamsize width (streamsize wide);
Get/set field width
The first form (1) returns the current value of the field width.
The second form (2) also sets a new field width for the stream.

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 at a point determined by the format flag adjustfield (one of left, right or internal).

The fill character can be retrieved or changed by calling the member function fill.

The format flag adjustfield can be modified by calling the member functions flags or setf, by inserting one of the following manipulators: left, right and internal, or by inserting the parameterized manipulator setiosflags.

The field width can also be modified using the parameterized manipulator setw.

参数

wide
New value for the stream's field width.
streamsize in signed integral type.

返回值

The value of the field width before the call.

示例

1
2
3
4
5
6
7
8
9
10
11
12
// field width
#include <iostream>     // std::cout, std::left

int main () {
  std::cout << 100 << '\n';
  std::cout.width(10);
  std::cout << 100 << '\n';
  std::cout.fill('x');
  std::cout.width(15);
  std::cout << std::left << 100 << '\n';
  return 0;
}

输出

100
       100
100xxxxxxxxxxxx


数据竞争

Accesses (1) or modifies (2) the stream object.
并发访问同一个流对象可能导致数据争用。

异常安全

基本保证:如果抛出异常,流处于有效状态。

另见