c++

C++中stod函数如何使用

小樊
153
2024-07-13 10:17:22
栏目: 编程语言

在C++中,std::stod函数用于将字符串转换为双精度浮点数。其函数原型如下:

double stod (const string& str, size_t* idx = 0);

其中,str是要转换的字符串,idx是可选参数,用于存储转换过程中的索引位置。

以下是一个示例代码,演示如何使用std::stod函数将字符串转换为双精度浮点数:

#include <iostream>
#include <string>

int main() {
    std::string str = "3.14159";
    double num = std::stod(str);
    
    std::cout << "The converted number is: " << num << std::endl;
    
    return 0;
}

在上面的示例中,我们将字符串"3.14159"转换为双精度浮点数,并将其输出到控制台。您可以根据需要自定义输入字符串并处理转换后的双精度浮点数。

0
看了该问题的人还看了