我可以使用Boost的格式库来替代iostream吗?
我不喜欢在 C++ 库中使用
。我更喜欢在
中使用类似于“printf”和“scanf”的内容。
我可以使用 Boost 的格式库来替换我所有 C++ 程序中的
吗?
I don't like to use <iostream>
in C++ Library. I prefer to use something that is similar to "printf" and "scanf" in <stdio.h>
.
Can I use Boost's format library to replace <iostream>
in all my C++ program ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Boost Format 仅执行格式化位。您仍然需要 iostream 才能真正使某些内容显示在屏幕上。
当然,将它们一起使用将实现与您正在寻找的
printf
的同等效果。而且它这样做并没有牺牲类型安全(尽管现在这不是一个大问题,因为编译器通常会警告错误的 printf 参数)。Boost Format only does the formatting bit. You still need iostream to actually make something appear on the screen.
Of course, using them together will achieve the parity with
printf
you are looking for. And it does so without sacrificing type-safety (though that's not a huge issue these days, since the compiler will usually warn about bad printf arguments).Boost.Format 可与 ostream 配合使用。您可以按如下方式使用它,
并根据需要
打印字符串。
Boost.Format 不适用于 istream。
Boost.Format works with ostreams. You use it as follows
or like
and the print the string as you want.
Boost.Format doesn't works with istreams.
您可以继续在 C++ 中使用
printf
,这没有什么问题。只需#include
即可开始。当然,iostream 有几个好处(类型安全是最大的好处) - 所以我仍然建议切换。You can continue to use
printf
in C++, nothing wrong with that. Just#include <cstdio>
and you're good to go. Of course,iostream
has several benefits (type-safety being the big one) - so I'd still recommend switching.printf 和 scanf 的局限性在于您无法将自己的对象与它们一起使用。
如果您使用 C++,您需要了解一些有关流的知识,因为这是该语言的标准。在 C++ 中,您可以为自己的对象创建流运算符,并让它们与流完美配合。
当然,您可以使用 boost::format,但它仅适用于格式化输出。我喜欢 boost::format,它非常有用,例如当您使用本地化(可翻译字符串)时。
printf and scanf are limited in the way you cannot use your own objects with them.
If you're using C++, you'll need to know a bit about streams, because that's the standard of the language. In C++ you can create stream operators for your own objects and have them work perfectly with streams.
Of course, you can use boost::format, but it will only be for formatted output. I like boost::format, and it's very useful, when you're using localisation for instance (translatable strings).
正如其他人给出的那样,简短的答案是肯定的,但 Boost 格式中没有与
scanf
等效的内容。然而,另一种方法是使用 Boost 的 Spirit< /a> 库,分别通过 Karma 和 Qi 组件具有输入和输出功能。不过,这对于您正在做的事情来说很可能是矫枉过正,因为它是一个完整的解析器/生成器实现。The short answer, as given by others, is yes, but there is no equivalent for
scanf
in Boost-format. However, an alternative is to use Boost's Spirit library which has both input and output capabilities via the Karma and Qi components, respectively. This may very well be overkill for what you are doing, though, as it is a full parser/generator implementation.