我试图使用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";发布于 2019-05-26 21:23:33
std::setw是一个很好的工具,可以打印不同长度的表格格式的数据。指定列的宽度,空格将自动填充,直到达到指定的列宽为止。但是,如果需要固定数量的空格,则更容易对其进行硬编码:cout << " "。如果多次需要相同的缩进,则可能需要将其定义为常量。
auto indent = string(3, ' ');
cout << indent << ...;这允许您在以后需要时轻松地调整缩进。
https://stackoverflow.com/questions/56317041
复制相似问题