std::setw
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <iomanip> で定義
|
||
/*unspecified*/ setw( int n ); |
||
式 out << setw(n) または in >> setw(n) で使用されたとき、ストリーム out または in の width パラメータをちょうど n に設定します。
引数
| n | - | 幅の新しい値 |
戻り値
str が std::basic_ostream<CharT, Traits> または std::basic_istream<CharT, Traits> 型のストリームの名前である場合に式 str << setw(n) または str >> setw(n) が以下のコードが実行されたかのように動作するような、未規定な型のオブジェクトを返します。
str.width(n);
ノート
ストリームの幅プロパティは以下の関数のいずれかが呼ばれた場合にゼロ (「未指定」を意味します) にリセットされます。
- 入力
- 出力
- basic_ostream::operator<<() のオーバーロード 1-7 (num_put::put() のステージ3)
- operator<<(basic_ostream&, char) および operator<<(basic_ostream&, char*)
- operator<<(basic_ostream&, basic_string&)
- std::put_money (money_put::put() の内部の)
- std::quoted (出力ストリームで使用されたとき)
この変更子が入力および出力に対して持つ正確な効果は個々の入出力関数の間で様々であり、それぞれの operator<< および operator>> のオーバーロードのページで個別に説明されます。
例
Run this code
#include <sstream>
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "no setw:" << 42 << '\n'
<< "setw(6):" << std::setw(6) << 42 << '\n'
<< "setw(6), several elements: " << 89 << std::setw(6) << 12 << 34 << '\n';
std::istringstream is("hello, world");
char arr[10];
is >> std::setw(6) >> arr;
std::cout << "Input from \"" << is.str() << "\" with setw(6) gave \""
<< arr << "\"\n";
}
出力:
no setw:42
setw(6): 42
setw(6), several elements: 89 1234
Input from "hello, world" with setw(6) gave "hello"
関連項目
| フィールド幅を管理します ( std::ios_baseのパブリックメンバ関数)
| |
| フィル文字を変更します (関数テンプレート) | |
| フィル文字の配置を設定します (関数) |