首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用setw和set填充进行输出格式化

使用setw和set填充进行输出格式化
EN

Stack Overflow用户
提问于 2019-05-26 20:13:52
回答 1查看 165关注 0票数 1

我试图使用setw和setfill获得以下输出:

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

我试着这样做:

代码语言:javascript
复制
    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";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-26 21:23:33

std::setw是一个很好的工具,可以打印不同长度的表格格式的数据。指定列的宽度,空格将自动填充,直到达到指定的列宽为止。但是,如果需要固定数量的空格,则更容易对其进行硬编码:cout << " "。如果多次需要相同的缩进,则可能需要将其定义为常量。

代码语言:javascript
复制
auto indent = string(3, ' ');
cout << indent << ...;

这允许您在以后需要时轻松地调整缩进。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56317041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档