有没有好的 C++ 例子?输入/输出使用

发布于 2024-08-09 05:40:17 字数 497 浏览 2 评论 0原文

我大量参与了 C++ 中的 I/O 工作(目前使用它来打印标题、表格、一些数据对齐),并且想知道它在开源项目或一般示例/nippets 中的正确/良好用法,

使用这样的东西:

cout.setf(ios::right,ios::jyustified);
cout<<std::setw()

std::copy (vector.begin(), vector.end(), std::osteam_iterator<const Foo *>
              std::cout,"\n");  //provided I have  operator <<  in/for Foo

locale mylocale(""); 
cout.imbue( mylocale );

我 不喜欢我当前的实现,因为我有很多强制 (\t)空格 来确保正确的缩进。因此我想看看顶级专业人士如何使用 I/O。

I am heavily involved in doing I/O in C++ (currently using it for printing headers, tables, some data alignments), and wonder about it proper/great usage in either open source projects or general examples/nippets

I use things such:

cout.setf(ios::right,ios::jyustified);
cout<<std::setw()

std::copy (vector.begin(), vector.end(), std::osteam_iterator<const Foo *>
              std::cout,"\n");  //provided I have  operator <<  in/for Foo

locale mylocale(""); 
cout.imbue( mylocale );

I don't like my current implementation, as I have a lot of forced (\t) and spaces to ensure correct indentation. Hence I want to see how I/O is used by top notch professionals.

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

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

发布评论

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

评论(3

堇色安年 2024-08-16 05:40:18

非常有用的一件事是 Boost IO 状态保护程序库。这提供了(特别是)一种处理“粘性”标志的干净方法。

不过,我倾向于同意 David Seiler 的观点——很少有人会对通过仅编写文本而获得的单一字体的纯文本等输出感到满意。然而,HTML 很容易生成。然而,RAII 可以很好地处理固定结构的文档,以确保您始终生成正确的嵌套标签。

One thing that's extremely useful is the Boost IO state saver library. This provides (in particular) a clean way to deal with "sticky" flags.

I tend to agree with David Seiler though -- very few people will be happy with output that's pure text in a single font, etc., that you get by writing just text. HTML, however, is quite easy to generate. RAII, however, can work nicely with fixed-structure documents to ensure that you always generate properly nested tags.

陌上青苔 2024-08-16 05:40:18

如果您想以某种易于阅读的方式格式化数据,请考虑发出某种结构化文档格式(例如 html 或 pdf),而不是纯文本加空格和制表符。这个问题不是 C++ I/O 特定的,并且不能通过巧妙使用 来真正解决。

If you want to format data in some easy-to-read way, consider emitting some kind of structured document format (e.g. html or pdf) instead of pure text-plus-spaces-and-tabs. That problem isn't C++ I/O specific, and can't really be solved with clever use of <stdio>.

红焚 2024-08-16 05:40:18

对于问题的缩进部分:“过滤器”的概念(例如在 Boost.IOStreams 库中实现)在这方面非常有用和强大。有关示例,请参阅此答案

For the indentation part of your question: The concept of "filter" (as implemented, for example, in the Boost.IOStreams library) is pretty useful and powerful in this respect. For an example, see this answer.

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