C++11 – std::string – stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型
在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异常
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11 – std::string – stod/stof/stoi/stol/stold/stoll/stoul/stoull,由std::string转换为int/long/float/double等其他类型
原文链接:https://www.stubbornhuang.com/1128/
发布于:2021年01月29日 10:55:26
修改于:2023年06月26日 21:57:43
当前分类随机文章推荐
- C++ - Windows下字符串UTF8编码转ANSI,ANSI转UTF8编码 阅读803次,点赞0次
- CMake - Windows系统设置CMake代理 阅读524次,点赞0次
- C++ - 对字符串和图片进行base64编解码 阅读1124次,点赞0次
- C++ - Windows和Linux系统下获取当前可执行程序的绝对路径 阅读3077次,点赞0次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读5175次,点赞0次
- GCC/G++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读5804次,点赞4次
- C++11 - std::function简要介绍以及可包装函数的几种形式总结 阅读3473次,点赞0次
- C++ - 我在项目实际开发中用到的第三方库/开源项目,涵盖网络、加密解密、GUI、网络、音视频、图片等等 阅读557次,点赞0次
- C++ - 单例模式 阅读88次,点赞0次
- C++11 - 基于无锁队列的单生产者单消费者模型 阅读6748次,点赞1次
全站随机文章推荐
- 左右手坐标系与旋转正向 阅读9100次,点赞1次
- Duilib - 使Duilib主窗口前置,但是又不是一直作为最顶层窗口 阅读3373次,点赞2次
- WordPress - 在文章页顶部加入百分比阅读滚动进度条 阅读1205次,点赞2次
- 计算机图形学 - 大白话模型视图投影矩阵(MVP)以及各种变换空间(模型空间、世界空间、相机空间、裁剪空间、屏幕空间) 阅读1905次,点赞1次
- 工具网站推荐 - 一些好用的latex公式识别工具和网站 阅读159次,点赞0次
- 资源分享 - Data Structures and Algorithms for Game Developers 英文高清PDF下载 阅读2020次,点赞0次
- Pytorch - 使用torch.matmul()替换torch.einsum('nctw,cd->ndtw',(a,b))算子模式 阅读3286次,点赞1次
- 资源分享 - Curves and Surfaces for Computer Graphics 英文高清PDF下载 阅读1464次,点赞0次
- 资源分享 - Game Physics Cookbook 英文高清PDF下载 阅读1998次,点赞0次
- VTK以批量三维点坐标为中心(点云)绘制球体,可用于标识特征点或者是化学分子 阅读5255次,点赞0次
评论
169