如何将std :: string_view转换为double? [英] How to convert std::string_view to double?

查看:145
本文介绍了如何将std :: string_view转换为double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a c++ parser for a custom option file for an application. I have a loop that reads lines in the form of option=value from a text file where value must be converted to double. In pseudocode it does the following:

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 的确切部分,但我无法确定找出从 std :: string_view 读取 double 的方法.

To handle the string splitting and passing around to functions, I use std::string_view because it avoids excessive copying and clearly states the intent of viewing segments of a pre-existing std::string. I've done everything to the point where std::string_view value_str points to the exact part of useful_statement that contains the value I want to extract, but I can't figure out the way to read a double from an std::string_view.

我知道 std :: stod 不适用于 std :: string_view .它可以让我写

I know of std::stod which doesn't work with std::string_view. It allows me to write

double value = std::stod(std::string(value_str));

但是,这很丑陋,因为它会转换为实际上不需要的字符串,即使在我看来,它不会产生明显的变化,但如果必须读取大量数字,它可能会太慢来自文本文件.

However, this is ugly because it converts to a string which is not actually needed, and even though it will presumably not make a noticeable difference in my case, it could be too slow if one had to read a huge amount of numbers from a text file.

另一方面, atof 不起作用,因为我不能保证空终止符.我可以通过在构造代码时将 \ 0 添加到 useful_statement 来对其进行破解,但这会使代码对读者造成混乱,并且如果代码被更改,则很容易被破坏/重构.

On the other hand, atof won't work because I can't guarantee a null terminator. I could hack it by adding \0 to useful_statement when constructing it, but that will make the code confusing to a reader and make it too easy to break if the code is altered/refactored.

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

So, what would be a clean, intuitive and reasonably efficient way to do this?

推荐答案

由于您使用C ++ 1z标记了问题,因此(从理论上来说)意味着您可以访问

Since you marked your question with C++1z, then that (theoretically) means you have access to from_chars. It can handle your string-to-number conversion without needing anything more than a pair of const char*s:

double dbl;
auto result = from_chars(value_str.data(), value_str.data() + value_str.size(), dbl);

当然,这需要您的标准库提供 from_chars 的实现.

Of course, this requires that your standard library provide an implementation of from_chars.

这篇关于如何将std :: string_view转换为double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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