如何将std :: string_view转换为double?

时间:2017-08-11 14:30:33

标签: c++ c++17 string-view

我正在为应用程序的自定义选项文件编写c ++解析器。我有一个循环,它从文本文件中以option=value的形式读取行,其中value必须转换为double。在伪代码中,它执行以下操作:

while(not EOF)
    statement <- read_from_file
    useful_statement <- remove whitespaces, comments, etc from statement
    equal_position <- find '=' in useful_statement
    option_str <- useful_statement[0:equal_position)
    value_str <- useful_statement[equal_position:end)
    find_option(option_str) <- double(value_str)

为了处理字符串拆分并传递给函数,我使用std::string_view,因为它避免了过度复制,并明确说明了查看预先存在的std::string段的意图。我已完成所有操作,std::string_view value_str指向包含我想要提取的值的useful_statement的确切部分,但我无法弄清楚如何阅读来自double的{​​{1}}。

我知道std::string_viewstd::stod无关。它允许我写

std::string_view

然而,这很丑陋,因为它转换为实际上不需要的字符串,即使它可能在我的情况下没有明显的差异,如果一个人必须阅读大量的数字,它可能会太慢来自文本文件。

另一方面,double value = std::stod(std::string(value_str)); 无法工作,因为我无法保证空终止符。我可以通过在构造它时将atof添加到\0来破解它,但是如果代码被更改/重构,这将使代码混淆给读者并使其容易破解。

那么,干净,直观且合理有效的方法是什么?

1 个答案:

答案 0 :(得分:1)

标题:

#include <boost/convert.hpp>
#include <boost/convert/strtol.hpp>

然后:

std::string x { "aa123.4"};
const std::string_view y(x.c_str()+2, 5); // Window that views the characters "123.4".

auto value = boost::convert<double>(y, boost::cnv::strtol());
if (value.has_value())
{
    cout << value.get() << "\n"; // Prints: 123.4
}

经过测试的编译器:

  • MSVC 2017

p.s。可以使用vcpkg轻松安装Boost(默认为32位,第二个命令为64位):

vcpkg install boost-convert
vcpkg install boost-convert:x64-windows