迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中使用 std::stod 系列函数

作者:迹忆客 最近更新:2023/03/24 浏览次数:

本文解释并演示了如何在 C++ 中使用 std::stod 系列函数的几种方法。


在 C++ 中使用 std::stodstring 转换为浮点值

std::stod 函数连同 std::stofstd::stold 是由 STL 提供的,用于将 string 转换为浮点数。请注意,我们说的是转换,但它更像是将字符串内容解释为浮点值。也就是说,这些函数使用预定义的解析规则扫描给定的字符串参数,以识别有效的浮点数并将它们存储在相应的类型对​​象中。

函数通过后缀来区分,后缀表示它们返回的类型。std::stod 对象返回 double 值,std::stof 返回一个浮点数,而 std::stold 返回 long double。这些函数自 C++11 起就成为 STL 的一部分,并包含在 <string> 头文件中。

在下面的代码片段中,我们将探索不同的转换场景并深入研究这些函数的解析规则。第一个示例是 string 对象仅包含数字和十进制分隔符 (.) 的最简单情况。std::stod 函数将给定的字符序列解释为有效的浮点数并将它们存储在 double 类型中。请注意,十进制分隔符不能是逗号(,)字符,因为它不会被视为数字的一部分。

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main() {
    string str1 = "123.0";
    string str2 = "0.123";

    auto m1 = std::stod(str1);
    auto m2 = std::stod(str2);

    cout << "std::stod("" << str1 << "") is " << m1 << endl;
    cout << "std::stod("" << str2 << "") is " << m2 << endl;

    return EXIT_SUCCESS;
}

输出:

std::stod("123.0") is 123
std::stod("0.123") is 0.123

或者,如果我们有 string 对象,其中数字与其他字符混合,则可以挑出两种常见情况。第一种情况:string 对象以数字开头,后跟其他字符; std::stod 函数在遇到第一个非数字字符(不包括十进制分隔符)之前提取起始数字。

第二种情况:string 参数以非数字字符开头,在这种情况下,函数会抛出 std::invalid_argument 异常并且无法进行转换。

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main() {
    string str3 = "123.4 with chars";
    string str4 = "chars 1.2";

    auto m3 = std::stod(str3);
//    auto m4 = std::stod(str4);

    cout << "std::stod("" << str3 << "") is " << m3 << endl;

    return EXIT_SUCCESS;
}

输出:

std::stod("123.0") is 123
std::stod("0.123") is 0.123

通常,std::stod 及其函数系列会丢弃起始空白字符。因此,如果我们传递一个带有多个前导空格字符后跟数字的字符串,则转换将成功执行,如下例所示。

此外,这些函数可以采用 size_t* 类型的可选参数,如果调用成功,它将存储处理的字符数。请注意,如果将字符串转换为有效的浮点数,则前导空格字符也会计数。

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main() {
    string str3 = "123.4 with chars";
    string str5 = "           123.4";

    size_t ptr1 = -1;
    size_t ptr2 = -1;

    auto m4 = std::stod(str3, &ptr1);
    auto m5 = std::stod(str5, &ptr2);

    cout << m4 << " - characters processed: " << ptr1 << endl;
    cout << "std::stod("" << str5 << "") is " << m5 << " - "
        << ptr2 << " characters processed" << endl;
    cout << "length: " << str5.size() << endl;

    return EXIT_SUCCESS;
}

输出:

123.4 - characters processed: 5
std::stod("           123.4") is 123.4 - 16 characters processed
length: 16

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便