C++11 – std::string – stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型
原创文章,作者:StubbornHuang,如若转载,请注明出处:《C++11 – std::string – stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型》https://www.stubbornhuang.com/1128/
在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异常
当前分类随机文章推荐
- C++11 - override关键字简要介绍
- C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点
- C++ 回调函数
- C++11/std::thread - 线程管理join/detach
- C++ - int转string方法总结
- C++ - vector存储动态指针时正确释放内存
- C++11/std::thread - 可作为线程函数的几种方式总结
- C++11 - std::function简要介绍以及可包装函数的几种形式总结
- C++ - 得到字符串中某个字符串出现的个数
- C++ - 字节数组byte与int的相互转换
全站随机文章推荐
- Duilib - 各个控件的消息响应类型
- OpenCV - 新建一个图片,并在图片上画由一点到另一点的直线,采用反走样形式
- OpenCV - 使用findContours()查找图片轮廓线,并将轮廓线坐标点输出
- 我的开源项目 - 支持C++11特性的定时器TinyTimer
- C++11 - std::chrono - 使用std::chrono::duration_cast进行时间转换,hours/minutes/seconds/milliseconds/microseconds相互转换,以及自定义duration进行转换
- 资源分享 - 白话深度学习与TensorFlow PDF下载
- WordPress - PhpStudy本地环境修改固定链接打不开网页404错误
- FFmpeg - RGB图像编码为h264出现垂直旋转的问题
- OpenCV - 静态图片人脸检测和摄像头人脸检测
- 资源分享 - 机器学习 (西瓜书) 周志华著PDF下载