setw() 有什么关系?

发布于 2024-11-05 06:04:32 字数 529 浏览 0 评论 0原文

我最近被以下事实所困扰: ios_base::width 和/或 setw 操纵器必须是 将每个项目写入流中进行重置

也就是说,你必须这样做:

while(whatever)
{
    mystream << std::setw(2) << myval;
}

而不是这样:

mystream.width(2);
while(whatever)
{
    mystream << myval;
}

好吧,好吧。

但有人知道为什么做出这个设计决定吗? 我是否缺少一些基本原理,或者这只是标准的一个黑暗角落?

其他流格式化修饰符(如链接的 SO 问题中提到的)是“粘性的”,而 setw 不是。

I recently was bitten by the fact that ios_base::width and/or the setw manipulator have to be reset with every item written to the stream.

That is, you must do this:

while(whatever)
{
    mystream << std::setw(2) << myval;
}

Rather than this:

mystream.width(2);
while(whatever)
{
    mystream << myval;
}

Ok, fine.

But does anyone know why this design decision was made?
Is there some rationale that I'm missing, or is this just a dark corner of the standard?

Other stream formatting modifiers (as mentioned in the linked SO question) are 'sticky', while setw is not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛清羽墨安 2024-11-12 06:04:32

哪些操纵器应该只影响下一个操作的决定似乎基于逻辑和经验观察,这些观察倾向于更好地考虑常见功能需求,因此程序员更容易编写和正确执行。

以下几点让我觉得相关:

  • some_stream << x 应该在大多数情况下都能正常工作,
  • 大多数设置宽度的代码将立即或不久之后传输该值,因此不相关的代码可以假设不会有一些“待处理”宽度值影响其输出
  • setfill() 不相关,除非有待处理的 setw(),因此不会对 some_stream << x 语句名列榜首
    • 仅当显式设置宽度时,程序员可以/必须根据他们对更大调用上下文的了解来考虑填充字符状态是否也合适
  • 一组值使用相同的填充字符是很常见的
  • 其他操纵符(如hexoct)是持久的,但它们的使用通常在代码块中,要么弹出先前的状态,要么(令人讨厌但更容易)将其设置回十进制

由此得出的答案的要点你的问题...

  • 如果setw()是持久的,则需要在每个流语句之间重置它以防止不必要的填充...

The decisions of which manipulators should affect only the next operation seem to be based on logical and empirical observations about what tends to factor common functional needs better, and hence be easier for the programmer to write and get right.

The following points strike me as relevant:

  • some_stream << x should just work right most of the time
  • most code that sets the width will immediately or very shortly afterwards stream the value, so unrelated code can assume there won't be some "pending" width value affecting its output
  • setfill() is not relevant unless there's a pending setw(), so won't adversely affect the some_stream << x statement topping our list
    • only when width is being explicitly set, the programmer can/must consider whether the fill character state is appropriate too, based on their knowledge of the larger calling context
  • it's very common for a set of values to use the same fill character
  • other manipulators like hex and oct are persistent, but their use is typically in a block of code that either pops the prior state or (nasty but easier) sets it back to decimal

The point leading from this that answers your question...

  • if setw() were presistent, it would need to be reset between each streaming statement to prevent unwanted fill...
送你一个梦 2024-11-12 06:04:32

我的看法是:如果您希望统一应用它,您可以随时执行如下操作。

int width =2;
while(whatever)
{
    mystream << std::setw(width) << myval;
}

但如果它像你提到的那样粘:

mystream.width(2);
while(whatever)
{
    mystream << myval;
}

如果我想要每行不同的宽度,我必须继续设置宽度。

所以本质上这两种方法几乎是相同的,我喜欢或不喜欢它们取决于我现在在做什么。

The way i see it is : You can always do something like below if you want it to be applied uniformly.

int width =2;
while(whatever)
{
    mystream << std::setw(width) << myval;
}

but if it was sticky as you mention:

mystream.width(2);
while(whatever)
{
    mystream << myval;
}

and if i wanted a different width every line I have to keep setting width.

So essentially both approaches are almost the same, and i would like or dislike them depending on what i am doing now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文