的boost ::格式的sprintf VS [英] boost::format vs sprintf

查看:216
本文介绍了的boost ::格式的sprintf VS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好:的boost ::格式的sprintf
我也想知道哪一个是更多的类型安全使用。

Which one is better: boost::format or sprintf? I also want to know which one is more type safe to use.

我知道与的sprintf 一个局限性在于它格式化一个的char [] 对象,大小的char [] 应足以容纳格式化字符串。但确实的boost ::格式也持同样的限制与否。任何人都可以指出我的网,我要提到的差别之中的boost ::格式的文档的sprintf

I know one limitation with sprintf that it formats a char[] object, and the size of char[] should be sufficient to hold the formatted string. But does boost::format also hold the same limitation or not. can Anyone point out me a doc on net which I would refer for the difference among boost::format and sprintf.

推荐答案

在稳健性和类型安全的boost ::格式而言是一个明显的赢家。因为它使用一种动态流它不具有与缓冲区大小的问题。类型的参数被保留在函数调用,从而格式化可以智能转换。这也意味着,没有拧紧了不匹配的类型的叠层的问题。类型转换也可在这个模型中。

In terms of robustness and type-safety boost::format is a clear winner. It doesn't have an issue with buffer size as it uses a dynamic stream. The types of the parameters are retained in the function call, thus the formatter can intelligently convert. This also means there is no problem of screwing up the stack with mismatched types. Type conversion is also available in this model.

格式的缺点是它的速度,并且它是有点麻烦。它的速度比的sprintf的格式的sprintf可以处理的低不少。它的语法也有点不是一个快速的调用sprintf的更加复杂。

The drawbacks of format are its speed and that it is a bit cumbersome. Its speed is quite a bit lower than that of sprintf for formats that sprintf can handle. Its syntax is also a bit more involved than a quick call to sprintf.

对于简单的令牌格式我通常用sprintf。对于复杂的字符串格式化我倾向于使用升压格式或输入输出流。

For simple token formatting I usually use sprintf. For complex string formatting I tend to use boost format, or iostreams.

这篇关于的boost ::格式的sprintf VS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆