std::ios_base::width
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Input/output library
Print functions (C++23)
Buffers
(C++23)
(C++98/26*)
(C++20)
Streams
Abstractions
File I/O
String I/O
Array I/O
(C++23)
(C++23)
(C++23)
(C++98/26*)
(C++98/26*)
(C++98/26*)
Synchronized Output
(C++20)
Types
Error category interface
(C++11)
(C++11)
std::ios_base
Member functions
Formatting
ios_base::width
Locales
Internal extensible array
Miscellaneous
Member classes
Member types
streamsize width() const;
(1)
streamsize width( streamsize new_width );
(2)
Manages the minimum number of characters to generate on certain output operations and the maximum number of characters to generate on certain input operations.
1) Returns the current field width.
2) Sets the field width to the given one. Returns the previous field width.
Contents
[edit] Parameters
new_width
-
new field width setting
[edit] Return value
The field width before the call to the function.
[edit] Notes
Some I/O functions call width(0) before returning, see std::setw (this results in this field having effect on the next I/O function only, and not on any subsequent I/O).
The exact effects this modifier has on the input and output vary between the individual I/O functions and are described at each operator<<
and operator>>
overload page individually.
[edit] Example
Run this code
#include <algorithm> #include <array> #include <iomanip> #include <iostream> #include <span> #include <string_view> using namespace std::string_view_literals; constexpr std::array table_header = { "Language"sv, "Author"sv, "Birthdate"sv, "RIP date"sv }; using row_t = std::array <std::string_view, table_header.size()>; using widths_t = std::array <std::size_t, table_header.size()>; constexpr std::array table_body = std::to_array <row_t> ({ {"C", "Dennis Ritchie", "1941年09月09日", "2011年10月12日"}, {"C++", "Bjarne Stroustrup", "1950年12月30日"}, {"C#", "Anders Hejlsberg", "1960年12月02日"}, {"Python", "Guido van Rossum", "1956年01月31日"}, {"Javascript", "Brendan Eich", "1961年07月04日"} }); constexpr widths_t calculate_column_widths(std::span <const row_t> table) { widths_t widths{}; for (const row_t& row : table) for (size_t i = 0; i != row.size(); ++i) widths[i] = std::max (widths[i], row[i].size()); return widths; } void print_row(const row_t& row, const widths_t& widths) { std::cout << '|'; for (size_t i = 0; i != row.size(); ++i) { std::cout << ' '; std::cout.width(widths[i]); std::cout << row[i] << " |"; } std::cout << '\n'; }; void print_break(const widths_t& widths) { const std::size_t margin = 1; std::cout.put('+').fill('-'); for (std::size_t w : widths) { std::cout.width(w + margin * 2); std::cout << '-' << '+'; } std::cout.put('\n').fill(' '); }; int main() { constexpr widths_t widths = calculate_column_widths(table_body); std::cout.setf(std::ios::left, std::ios::adjustfield); print_break(widths); print_row(table_header, widths); print_break(widths); for (const row_t& row : table_body) print_row(row, widths); print_break(widths); }
Output:
+------------+-------------------+------------+------------+ | Language | Author | Birthdate | RIP date | +------------+-------------------+------------+------------+ | C | Dennis Ritchie | 1941年09月09日 | 2011年10月12日 | | C++ | Bjarne Stroustrup | 1950年12月30日 | | | C# | Anders Hejlsberg | 1960年12月02日 | | | Python | Guido van Rossum | 1956年01月31日 | | | Javascript | Brendan Eich | 1961年07月04日 | | +------------+-------------------+------------+------------+