gpt4 book ai didi

c++ - 流填充字符的默认定位

转载 作者:行者123 更新时间:2023-12-02 10:00:33 25 4
gpt4 key购买 nike

std::setfill 的引用页面上,有一个类似下面程序的例子:

#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setfill('*') << std::setw(10) << 42 << '\n';
}
它声称它应该打印:
********42
这表明填充字符的默认位置就像 std::right 使用了机械手。
标准是否保证这是行为,还是我需要指定 std::right为了确定?
此外,这是否仅适用于 std::cout ,还是任何输出流都一样?

最佳答案

setfill[std.manip] 中定义作为

template<class charT, class traits>
void f(basic_ios<charT, traits>& str, charT c) {
// set fill character
str.fill(c);
}
所以我们需要看看 ostream 会发生什么的 fill功能,详细信息请参见 [ostream.formatted.reqmts]/3作为

If a formatted output function of a stream os determines padding, it does so as follows. Given a charT character sequence seq where charT is the character type of the stream, if the length of seq is less than os.width(), then enough copies of os.fill() are added to this sequence as necessary to pad to a width of os.width() characters. If (os.flags() & ios_­base​::​adjustfield) == ios_­base​::​left is true, the fill characters are placed after the character sequence; otherwise, they are placed before the character sequence.


所以除非 left指定,填充字符在前。默认情况下, left不是 cout 的设置标志这是 Table 122: basic_­ios​::​init() effects   [tab:basic.ios.cons] 中的详细信息

关于c++ - 流填充字符的默认定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62703952/

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