簡體   English   中英

使用setw和setfill進行輸出格式化

[英]Output formatting using setw and setfill

我正在嘗試使用setw和setfill獲得以下輸出:

OPTIONS:
    <expression>
       The usual operators +, -, *, / and % (remainder)
       Expressions are fixed-point decimal numbers, and
       Parentheses () and corchetes {} may be used for grouping.

我正在嘗試這樣:

    cout << "OPTIONS:" << '\n';
    cout << "\t<expression>\n";
    cout << '\t' << setw(3) << setfill(' ') << "The usual operators +, -, *, / and % (remainder)\n";
    cout << '\t' << setw(3) << setfill(' ') << "Expressions are fixed-point decimal numbers, and\n";
    cout << '\t' << setw(3) << setfill(' ') << "Parentheses () and corchetes {} may be used for grouping.\n\n";

std::setw是用於以表格格式打印長度可變的數據的出色工具。 您指定列的寬度,空格將自動填充,直到達到指定的列寬。 但是,如果您需要固定數量的空格, cout << " "容易對其進行硬編碼: cout << " " 如果多次需要相同的縮進,則可能需要將其定義為常量

auto indent = string(3, ' ');
cout << indent << ...;

這使您可以在以后根據需要輕松調整縮進量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM