float < - > std :: string转换替代?

ura*_*ray 8 c++ string floating-point stl

没有任何替代品atof,strtod,lexical_cast,stringstreamsprintf

那是:

  1. 快速
  2. C++方式(std::string代替char*)
  3. 安全(没有缓冲区溢出风险)
  4. 有效(如果无法进行转换,则返回NaN)
  5. 没有外部库(独立)

我更喜欢这个,一个简单的功能,优化,并在一定程度上

原因:

  • atof并且strtod是C函数,它们不会NaN在失败时返回,我更喜欢继续工作std::string,所以我只是问是否有人已经写了一些std::string我可以使用的包装器(如果你不介意的话).
  • lexical_cast 有依赖性
  • stringstream 是慢的
  • sprintf 有缓冲区溢出风险及其C函数

seh*_*ehe 1

我会看一下《提升精神》

至少格式化程序的基准(即 float -> string)始终是最重要的* 1 *

此外,可以使用策略类很好地配置解析时的确切输入格式规范和语义。


这是我对 qi::any_real_parser<> 的绝对最小依赖使用以及它涉及的依赖列表:

#include <boost/spirit/include/qi_real.hpp>

namespace qi = boost::spirit::qi;

int main()
{
    const char input[] = "3.1415926";
    const char *f(input);
    const char *l(f+strlen(input));

    qi::any_real_parser<double> x;
    double parsed;
    x.parse(f, l, qi::unused, qi::unused, parsed);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 推动/概念
  • 增强/配置
  • 增强/细节
  • 提升/异常
  • 增强/融合
  • 提升/迭代器
  • 提升/数学
  • 升压/mpl
  • 增强/可选
  • 升压/预处理器
  • 增强/原型
  • 升压/范围
  • 增强/正则表达式
  • 提升/精神
  • 提升/类型
  • 增强/类型特征
  • 升压/效用
  • 增强/变体

aligned_storage.hpp,assert.hpp,blank_fwd.hpp,blank.hpp,call_traits.hpp,checked_delete.hpp,concept_check.hpp,config.hpp,cstdint.hpp,current_function.hpp,foreach_fwd.hpp,foreach.hpp,get_pointer。 hpp,implicit_cast.hpp,iterator.hpp,limits.hpp,math_fwd.hpp,next_prior.hpp,noncopyable.hpp,none.hpp,none_t.hpp,可选.hpp,ref.hpp,static_assert.hpp,swap.hpp, throw_exception.hpp,type.hpp,utility.hpp,variant.hpp,version.hpp

1例如http://www.boost.org/doc/libs/1_47_0/libs/spirit/doc/html/spirit/karma/performance_measurements/numeric_performance/double_performance.html