函式
<iomanip>

std::setw

/*undefined*/ setw (int n);
設定欄位寬度
設定要用於輸出操作的欄位寬度

如同在它被插入/提取的流上呼叫成員函式 width 並將 n 作為引數一樣(它可以被插入/提取到輸入流輸出流)。

此操縱符在標頭檔案 <iomanip> 中宣告。

引數

n
用作欄位寬度的字元數。

返回值

未指定。此函式只應作為流操縱符使用(見示例)。

示例

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;
}

輸出
        77


資料競爭

它被插入/提取到的流物件會被修改。
對同一流物件的併發訪問可能會引發資料競爭。

異常安全

基本保證:如果丟擲異常,流處於有效狀態。

另見