gpt4 book ai didi

C++ ostream 填充添加尾随 0

转载 作者:行者123 更新时间:2023-11-30 02:20:20 25 4
gpt4 key购买 nike

您好,我在使用 C++ 中的 ostream 对象格式化输出时遇到问题。

ostream& write(ostream& os)
{
os << os.fill('*') << os.width(10);
return os;
}

输出结果如下:

********* 0

我试图在没有尾随 0 的情况下实现 10 个 *

尾随 0 的原因是什么?

我是否错误地使用了宽度和填充函数?

最佳答案

正如 ios::base::width 中所说:

Return value : The value of the field width before the call.

所以你正在做的是用*sfill你的ostream,然后在调用之前打印字段的值0

还有一件事你可能没有注意到,在 0 之前,你有另一个字符,那是因为 std::ios::fill返回值:

Return value : The value of the fill character before the call.

试试这个:

ostream& write(ostream& os)
{
os.fill('*');
os.width(10);
return os;
}

关于C++ ostream 填充添加尾随 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49868956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com