gpt4 book ai didi

c++ - 为什么某些 C++ 标准库 `operator<<` 函数将其流宽度重置为 0?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:17:44 24 4
gpt4 key购买 nike

如 C++ 标准的 [ostream.inserters.character] 部分所述,在将 char 或字符串插入流后,流的宽度设置为0:

template<class traits>
basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>& out,
const unsigned char* s);
Do lots of sensible and expected things...
Calls width(0).

为什么 C++ 标准要求调用 width(0)

更改流宽度而不将其重置为原始值的合理性是什么?(据我所知,流的所有其他属性都由流插入运算符保留。)

有个相关问题here ,这说明了这引起的困惑,但没有解释为什么标准规定了这种行为。

最佳答案

好像不是“一些”operator<<功能,一般来说是插入器和提取器 (21.4.8.9)。参见 c++ standard和一个合理的推理 Tony D's answer

关于c++ - 为什么某些 C++ 标准库 `operator<<` 函数将其流宽度重置为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22005035/

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