在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