【问题标题】:What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?std::to_string、boost::to_string 和 boost::lexical_cast<std::string> 之间有什么区别?
【发布时间】:2015-04-01 19:20:30
【问题描述】:

boost::to_string(在boost/exception/to_string.hpp 中找到)的用途是什么?它与boost::lexical_cast&lt;std::string&gt;std::to_string 有何不同?

【问题讨论】:

  • 如果我没记错的话,boost::to_stringstd::to_string 之前,这通常是这种情况。在被标准库接受之前,Boost 往往是一些东西的游乐场。 std::to_string 自 C++11 起是新的。
  • @Cyber​​:我建议您将其写为(部分)答案。
  • @CoryKramer, boost::to_string 可能更老,但它与std::to_string 不同,std::to_string 不是基于它(他们只是碰巧使用了相同的名称)。

标签: c++ boost std tostring lexical-cast


【解决方案1】:

std::to_string,自 C++11 起可用,专门用于基本数字类型。它还有一个std::to_wstring 变体。

它旨在产生与sprintf 相同的结果。

您可以选择这种形式来避免对外部库/头文件的依赖。


throw-on-failure 函数boost::lexical_cast&lt;std::string&gt; 和它的非throw 表亲boost::conversion::try_lexical_convert 适用于可以插入std::ostream 的任何类型,包括来自其他库或您的自己的代码。

针对常见类型存在优化的特化,其通用形式类似于:

template< typename OutType, typename InType >
OutType lexical_cast( const InType & input ) 
{
    // Insert parameter to an iostream
    std::stringstream temp_stream;
    temp_stream << input;

    // Extract output type from the same iostream
    OutType output;
    temp_stream >> output;
    return output;
}

您可以选择这种形式来利用泛型函数中输入类型的更大灵活性,或者从您知道不是基本数字类型的类型生成std::string


boost::to_string 没有直接记录,似乎主要供内部使用。它的功能类似于lexical_cast&lt;std::string&gt;,而不是std::to_string

【讨论】:

    【解决方案2】:

    还有更多区别:boost::lexical_cast 在将双精度转换为字符串时工作方式有些不同。请考虑以下代码:

    #include <limits>
    #include <iostream>
    
    #include "boost/lexical_cast.hpp"
    
    int main()
    {
        double maxDouble = std::numeric_limits<double>::max();
        std::string str(std::to_string(maxDouble));
    
        std::cout << "std::to_string(" << maxDouble << ") == " << str << std::endl;
        std::cout << "boost::lexical_cast<std::string>(" << maxDouble << ") == "
                  << boost::lexical_cast<std::string>(maxDouble) << std::endl;
    
        return 0;
    }
    

    结果

    $ ./to_string
    std::to_string(1.79769e+308) == 179769313486231570814527423731704356798070600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    boost::lexical_cast<std::string>(1.79769e+308) == 1.7976931348623157e+308
    

    如您所见,boost 版本使用指数表示法 (1.7976931348623157e+308),而 std::to_string 打印每个数字和六个小数位。对于您的目的,一个可能比另一个更有用。我个人觉得 boost 版本更具可读性。

    【讨论】:

    • 除了粘贴一些代码之外,请多做一些事情。描述您的代码做什么以及如何做到这一点。
    • 对于相同的双精度值,to_string() 和 lexical_cast() 的输出很简单;-))
    • 这些结果肯定是不同的——这值得注意。但对我来说,哪个结果更“正确”并不明显。
    【解决方案3】:

    这是我找到的整数到字符串转换的基准,希望它不会对 float 和 double Fast integer to string conversion benchmark in C++ 发生太大变化。

    【讨论】:

    • 您的链接可能很有趣,但还达不到回答问题的程度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2018-08-23
    • 2016-05-12
    • 1970-01-01
    相关资源
    最近更新 更多