std::ios_base::width
提供: cppreference.com
<tbody>
</tbody>
streamsize width() const; |
(1) | |
streamsize width( streamsize new_width ); |
(2) | |
特定の出力操作で生成される最小文字数と特定の入力操作で生成される最大文字数を管理します。
1) 現在のフィールド幅。
2) フィールド幅を指定された値に設定します。 以前のフィールド幅を返します。
引数
| new_width | - | 新しいフィールド幅の設定 |
戻り値
関数を呼ぶ前のフィールド幅。
ノート
いくつかの入出力関数は戻る前に width(0) を呼びます。 std::setw を参照してください (つまり、このフィールド幅は次の入出力関数にのみ効果を持ち、いかなる後続の入出力にも影響しないことを意味します)。
フィールド幅の正確な効果は個々の入出力関数ごとに様々であり、 operator<< と operator>> の個々のオーバーロードのページで説明されます。
例
Run this code
#include <array>
#include <tuple>
#include <ctime>
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
auto str_time = [](int year, int mon, int day)
{
constexpr std::array<const char*, 7> week_day{ {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
} };
std::tm tm{};
tm.tm_year = year - 1900;
tm.tm_mon = mon - 1;
tm.tm_mday = day;
day += mon < 3 ? year-- : year - 2;
tm.tm_wday = (23 * mon / 9 + day + 4 + year / 4 - year / 100 + year / 400) % 7;
std::ostringstream out;
out << week_day[tm.tm_wday] << ", " << std::put_time(&tm, "%B %d, %Y");
return out.str();
};
constexpr int column_size = 4;
using table_t = std::array<std::string, column_size>;
table_t headers{ { "Name", "Birthdate", "Death date", "Language Created" } };
std::array<table_t, 5> data{ {
{ { "Dennis MacAlistair Ritchie", str_time(1941, 9, 9), str_time(2011, 10, 12), "C" } },
{ { "Bjarne Stroustrup", str_time(1950, 12, 30), "", "C++" } },
{ { "Anders Hejlsberg", str_time(1960, 12, 2), "", "C#" } },
{ { "Guido van Rossum", str_time(1956, 1, 31), "", "Python" } },
{ { "Brendan Eich", str_time(1961, 7, 4), "", "Javascript" } }
} };
constexpr int name_wid = 30;
constexpr int birth_wid = 30;
constexpr int death_wid = 30;
constexpr int lang_wid = 18;
auto print_line = [](table_t const &tbl)
{
auto const &[Name, Birthdate, DeathDate, LanguageCreated] = tbl;
std::cout.width(name_wid);
std::cout << ("| " + Name) << '|';
std::cout.width(birth_wid);
std::cout << (' ' + Birthdate) << '|';
std::cout.width(death_wid);
std::cout << (' ' + DeathDate) << '|';
std::cout.width(lang_wid);
std::cout << (' ' + LanguageCreated) << '|';
std::cout << '\n';
};
constexpr int total_wid = name_wid + birth_wid + death_wid + lang_wid + column_size;
auto print_break = []
{
std::cout.width(total_wid);
std::cout.fill('-');
std::cout << '-' << std::endl;
std::cout.fill(' ');
};
std::cout.setf(std::ios::left, std::ios::adjustfield);
print_break();
print_line(headers);
print_break();
for (auto const &entry : data)
print_line(entry);
print_break();
}
出力:
----------------------------------------------------------------------------------------------------------------
| Name | Birthdate | Death date | Language Created |
----------------------------------------------------------------------------------------------------------------
| Dennis MacAlistair Ritchie | Tuesday, September 09, 1941 | Wednesday, October 12, 2011 | C |
| Bjarne Stroustrup | Saturday, December 30, 1950 | | C++ |
| Anders Hejlsberg | Friday, December 02, 1960 | | C# |
| Guido van Rossum | Tuesday, January 31, 1956 | | Python |
| Brendan Eich | Tuesday, July 04, 1961 | | Javascript |
----------------------------------------------------------------------------------------------------------------
関連項目
| 浮動小数点操作の10進精度を管理します (パブリックメンバ関数) |