使用Boost Karma将std :: stringstream替换为double到std :: string的转换 [英] Using Boost Karma to replace std::stringstream for double to std::string Conversion

查看:52
本文介绍了使用Boost Karma将std :: stringstream替换为double到std :: string的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了提高性能,我希望替换:

For performance improvement, I am looking to replace:

template<class T> std::string ToStringFixed(const T x, const unsigned int width = 8)
{
  std::stringstream ss;
  ss << std::setprecision(width) << std::fixed << x;
  return ss.str();
}

通过Boost Karma的实现,因为我们的项目已经使用了Boost,并且与上面的天真的解决方案相比,它似乎具有显着的性能提升.

With an implementation from Boost Karma as our project already uses Boost and it looks to have a significant performance gain over the naive solution above.

类似的东西:

std::string ToString(double d)
{
  using boost::spirit::karma::double_;
  using boost::spirit::ascii::space;
  using boost::spirit::karma::generate;

  std::string s
  std::back_insert_iterator<std::string> sink(s);
  generate(sink, double_, d);
  return s;
}

采用以下方式: http://thisthread.blogspot.com/2011/04/generating-text-with-spirit-karma.html 似乎是在正确的轨道上,但是我不清楚如何控制精度,或者是否可以控制精度对于浮点类型,解决方案可以是模板友好的,而无需使用类型特征.(可接受的使用C ++ 14的答案.)

Which is adopted from: http://thisthread.blogspot.com/2011/04/generating-text-with-spirit-karma.html seems to be on the right track, but it isn't clear to me how to control the precision, or if such a solution can be template-friendly for floating point types without use of type traits. (Answers using up through C++14 are acceptable.)

推荐答案

您可以在

You can achieve what you want with the help of real_generator's formatting policies. Since you only need to modify the precision you can derive from the default real_policies<Num> and then add the precision(Num n) member function that has the behaviour you need.

在WandBox上运行

#include <iostream>
#include <string>
#include <cmath>

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

template <typename Num>
struct precision_policy : boost::spirit::karma::real_policies<Num>
{
    precision_policy(int prec):precision_(prec){}
    int precision(Num n) const { return precision_; }
    int precision_;
};

std::string ToStringFixedKarma(double d, const unsigned int width = 8)
{
  using boost::spirit::karma::real_generator;
  using boost::spirit::ascii::space;
  using boost::spirit::karma::generate;

  real_generator<double,precision_policy<double> > my_double_(width);

  std::string s;
  std::back_insert_iterator<std::string> sink(s);
  generate(sink, my_double_, d);
  return s;
}

int main() {
    const double pi = std::acos(-1);
    std::cout << ToStringFixedKarma(pi) << std::endl;
    std::cout << ToStringFixedKarma(pi,2) << std::endl;
    std::cout << ToStringFixedKarma(pi,4) << std::endl;
}

PS:文档(并检查源代码)似乎暗示这些策略中的成员函数必须是静态的,这将使您无法实现所需的功能,但必须为

PS: The documentation (and inspecting the source) seems to imply that the member functions in these policies need to be static, which would make what you need impossible, but as this example shows, that is not the case.

这篇关于使用Boost Karma将std :: stringstream替换为double到std :: string的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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