C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- è¾å ¥/è¾åºæçºµç¬¦
- std::ios_base
- std::ios_base::failure
- std::ios_base::event_callback
- std::ios_base::seekdir
- std::ios_base::event
- std::ios_base::fmtflags
- std::ios_base::iostate
- std::ios_base::iword
- std::ios_base::pword
- std::ios_base::register_callback
- std::ios_base::sync_with_stdio
- std::ios_base::Init
- std::ios_base::openmode
- std::ios_base::precision
- std::ios_base::width
- std::ios_base::imbue
- std::ios_base::getloc
- std::ios_base::xalloc
- std::ios_base::ios_base
- std::ios_base::~ios_base
- std::ios_base::flags
- std::ios_base::setf
- std::ios_base::unsetf
- C 飿 ¼æä»¶è¾å ¥/è¾åº
- std::basic_streambuf
- std::basic_filebuf
- std::basic_stringbuf
- std::strstreambuf
- std::basic_syncbuf
- std::basic_ios
- std::basic_istream
- std::basic_osyncstream
- std::basic_ostream
- std::basic_iostream
- std::basic_ifstream
- std::basic_ofstream
- std::basic_fstream
- std::basic_istringstream
- std::basic_ostringstream
- std::basic_stringstream
- std::istrstream
- std::ostrstream
- std::strstream
- std::streamoff
- std::streamsize
- std::fpos
- std::iostream_category
- std::io_errc
- std::cin, std::wcin
- std::cout, std::wcout
- std::cerr, std::wcerr
- std::clog, std::wclog
- 注é
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >è¾å ¥/è¾åºåº >std::ios_base > std::ios_base::width
std::ios_base::width
| streamsize width() const; |
(1) | |
| streamsize width( streamsize new_width ); |
(2) | |
管çæäºè¾åºæä½æ¶çæçæå°å符æ°ï¼åæäºè¾åºæä½æ¶çæçæå¤§å符æ°ã
1) è¿åå½åå宽ã
2) 设置å宽为ç»å®å¼ãè¿åå
åçä½å®½ã
åæ°
| new_width | - | è®¾ç½®çæ°å宽 |
è¿åå¼
è°ç¨å½æ°åçå宽
注æ
ä¸äº I/O 彿°å¨è¿ååè°ç¨ width(0) ï¼è§ std::setw ï¼è¿å¯¼è´æ¤åä» å¨ä¸æ¬¡ I/O 彿°ï¼èä¸åä»»ä½åç»§ I/O æ¶ææï¼ã
æ¤ä¿®æ¹å¨å¨è¾å
¥åè¾åºä¸æ¥æçåç¡®ææå¨åç¬ç I/O 彿°ä¹é´æå«ï¼ææåç¬æè¿°äºæ¯ä¸ª operator<< å operator>> éè½½ç页é¢ã
示ä¾
è¿è¡æ¤ä»£ç
#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 | ----------------------------------------------------------------------------------------------------------------
åé
| ç®¡çæµ®ç¹æä½ç精度 (å ¬å¼æå彿°) |