C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull

本文详细介绍了C++11引入的std::string转换函数,如std::stod、std::stof、std::stoi等,用于将字符串转换为不同类型的数值。这些函数不仅提供了方便的转换方式,还在转换失败或数值超出范围时抛出异常,提高了代码的健壮性。通过示例代码展示了如何利用idx参数处理包含多个数值的字符串。
摘要由CSDN通过智能技术生成

在C++11发布之前我们只能使用C语言的atoi等函数完成字符串到int/float/double等类型的转换,而在C++11中在std::string中自带了stoi/stod等工具函数进行转换!

1 std::stod

  • 函数原型
double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
  • 函数功能
    将std::string字符串转化为双精度类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。
  • 函数返回值
    如果成功则返回转换的double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

这里说明一下参数idx的意义和作用:
如果当我们的字符串中只有一个double型数值时我们可以这样进行转化:

#include <iostream>
#include <string>

int main()
{
	std::string numStr = "5646545.32";

	double number1 = std::stod(numStr);

	getchar();
	return 0;
}

但是如果字符串中有两个double型的数值该如何进行转化呢?这个时候我们就可以使用参数idx存储上一个double数值解析完成之后的在字符串中的索引,代码如下:

#include <iostream>
#include <string>

int main()
{
	std::string numStr = "5646545.32 3.1415923";
	size_t idx = 0;

	double number1 = std::stod(numStr, &idx);
	double number2 = std::stod(numStr.substr(idx));

	getchar();
	return 0;
}

2 std::stof

  • 函数原型
float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
  • 函数功能
    将std::string字符串转换为float浮点类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似。
  • 函数返回值
    如果成功则返回转换的float型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

3 std::stoi

  • 函数原型
int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
  • 函数功能
    将std::string字符串转换为int类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似。
    • base : 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0
  • 函数返回值
    如果成功则返回转换的int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

4 std::stol

  • 函数原型
long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
  • 函数功能
    将std::string字符串转换为long int类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似
    • base : 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0
  • 函数返回值
    如果成功则返回转换的long int型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常。

5 std::stold

  • 函数原型
long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);
  • 函数功能
    将std::string字符串转换为long double类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似
  • 函数返回值
    如果成功则返回转换的long double型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

6 std::stoll

  • 函数原型
long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
  • 函数功能
    将std::string字符串转换为long long类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似
    • base : 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0
  • 函数返回值
    如果成功则返回转换的long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

7 std::stoul

  • 函数原型
unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
  • 函数功能
    将std::string字符串转换为unsigned long类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似
    • base : 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0
  • 函数返回值
    如果成功则返回转换的unsigned long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

8 std::stoull

  • 函数原型
unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
  • 函数功能
    将std::string字符串转换为unsigned long long类型

  • 函数参数

    • str : 待转换的字符串
    • idx : 如果idx的指针不为空,则该函数会将idx的值设置为str当前解析完成的数值字符串之后的下一个字符的位置。意义与作用与stdb中的idx类似
    • base : 转换字符所使用的进制数,如果为0,则使用的进制数由字符串的格式决定,默认值为10而不是0
  • 函数返回值
    如果成功则返回转换的unsigned long long型数值,如果转换失败,则会抛出invalid_argument异常,如果待转换的字符所代表的数值超出数值类型范围的两倍,则会抛出out_of_range异常

如果大家对计算几何或者计算机图形学感兴趣或者需要下载相关书籍,可以访问我的个人站:http://www.stubbornhuang.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HW140701

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值