前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++中的std::stod, stCPP程序说明std::stod():stof, std::stold

c++中的std::stod, stCPP程序说明std::stod():stof, std::stold

作者头像
鲸落c
发布2022-12-14 13:56:43
2.5K0
发布2022-12-14 13:56:43
举报
文章被收录于专栏:鲸落学习笔记鲸落学习笔记

std: :stod() : 它将字符串转换为双精度。 语法:

代码语言:javascript
复制
double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: 返回double类型的值
参数
str : 要转换的字符串
pos : 存储处理的字符数的整数的地址。此参数也可以是空指针,在这种情况下不使用它。
代码语言:javascript
复制
// CPP程序说明std::stod()
#include <string>
#include <iostream>

int main(void)

{
    std::string str = "y=4.4786754x+5.6";

    double y, x, a, b;
    y = 0;
    x = 0;

    // 偏移量将设置为“值”-1的字符长度。
    std::size_t offset = 0;

    a = std::stod(&amp;str[2], &amp;offset);
    b = std::stod(&amp;str[offset + 3]);

    std::cout << b;
    return 0;
}

输出:

代码语言:javascript
复制
5.6

再比如:

代码语言:javascript
复制
// CPP程序说明std::stod()
#include <iostream>
#include <string>
using namespace std;
int main()
{

    string b = "5";
    double a = stod(b);
    int c = stoi(b);
    cout << b << " " << a << " " << c << endl;
}

输出:

代码语言:javascript
复制
5 5 5

如果未执行转换,则会引发invalid_argument异常。如果读取的值超出双精度的可表示值范围,则会引发out_of_range异常。无效的 idx 会导致未定义的行为。

标准::STOF : 它将字符串转换为浮点数。 语法:

代码语言:javascript
复制
float stof( const string&amp; str, size_t* pos = 0 );
float stof( const wstring&amp; str, size_t* pos = 0 );
参数
str : 要转换的字符串
pos : 用于存储已处理字符数的整数的地址此参数也可以是空指针,在这种情况下,不使用此参数。
Return value: 返回float类型的值。

示例 1:

代码语言:javascript
复制
// CPP程序说明std::stof()
#include <iostream>
#include <string>
int main()
{
    std::string x;
    x = "20";
    float y = std::stof(x) + 2.5;
    std::cout << y;
    return 0;
}

输出:

代码语言:javascript
复制
22.5

示例 2:

代码语言:javascript
复制
// CPP程序说明std::stof()
#include <iostream>
#include <string>
int main()
{
    std::string str = "5000.5";
    float x = std::stof(str);
    std::cout << x;
    return 0;
}

输出:

代码语言:javascript
复制
5000.5

如果无法执行转换,则会引发invalid_argument异常。

标准::告诉 : 它将字符串转换为长双精度。 语法:

代码语言:javascript
复制
long double stold( const string&amp; str, size_t *pos = 0 );
long double stold (const wstring&amp; str, size_t* pos = 0);
参数 : 
str : 要转换的字符串
pos : 存储第一个未转换字符的索引的整数地址。
此参数也可以是空指针,在这种情况下不使用它。
Return value : 它返回longdouble类型的值。

示例 1:

代码语言:javascript
复制
// CPP程序说明std::stold()
#include <iostream>
#include <string>
int main()
{
    std::string str = "500087";
    long double x = std::stold(str);
    std::cout << x;
    return 0;
}

输出:

代码语言:javascript
复制
500087

示例 2:

代码语言:javascript
复制
// CPP程序说明std::stold()
#include <iostream>
#include <string>
int main()
{
    std::string x;
    x = "2075";
    long double y = std::stof(x) + 2.5;
    std::cout << y;
    return 0;
}

输出:

代码语言:javascript
复制
2077.5
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档