我正在为应用程序的自定义选项文件编写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_view
与std::stod
无关。它允许我写
std::string_view
然而,这很丑陋,因为它转换为实际上不需要的字符串,即使它可能在我的情况下没有明显的差异,如果一个人必须阅读大量的数字,它可能会太慢来自文本文件。
另一方面,double value = std::stod(std::string(value_str));
无法工作,因为我无法保证空终止符。我可以通过在构造它时将atof
添加到\0
来破解它,但是如果代码被更改/重构,这将使代码混淆给读者并使其容易破解。
那么,干净,直观且合理有效的方法是什么?
答案 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
}
经过测试的编译器:
p.s。可以使用vcpkg轻松安装Boost(默认为32位,第二个命令为64位):
vcpkg install boost-convert
vcpkg install boost-convert:x64-windows