【问题标题】:How to use setfill() in <iomanip> more fluently?如何更流畅地使用 <iomanip> 中的 setfill()?
【发布时间】:2021-02-05 01:22:12
【问题描述】:

今天学习了setfill()#include&lt;iomanip&gt;中的其他方法,于是有了使用#include&lt;iomanip&gt;的练习。

我想在控制台上打印这个:

+--------------+ | Iomanip 示例 | +--------------+

所以我写了这段代码:

cout << '+' << setfill('-') << setw(25) << '+' << endl;
cout << setw(26) << internal << "Iomanip Examples" << endl;
cout << '+' << setfill('-') << setw(25) << '+' << endl;

但我的控制台打印了这个:

+---------------+ //没关系。 ------Iomanip 例子 //nope.. +---------------+

我认为当我使用setfill('-') 时,输出流中的空格会被'-' 字符填充。

我该如何解决这个问题?

【问题讨论】:

    标签: c++ iomanip


    【解决方案1】:

    您再次需要在第二行使用setfill(' '),就像您的情况一样。 一旦我们使用setfill(),它将在我们使用setw()的程序中设置为所有时间。

    【讨论】:

    • 谢谢。现在我意识到了我的问题。我认为'internal'关键字有助于将字符串排列在输出流的中间,但事实并非如此。
    • 示例)std::cout
    • @Samuel per this reference: “修改填充字符的默认位置。 leftright 适用于任何输出,internal 适用于整数、浮点和货币输出”因此在您的示例中没有任何效果。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多