C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull
文章目录
- 1 std::stod
- 2 std::stof
- 3 std::stoi
- 4 std::stol
- 5 std::stold
- 6 std::stoll
- 7 std::stoul
- 8 std::stoull
在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
C++11 - std::string - stod/stof/stoi/stol/stold/stoll/stoul/stoull相关推荐
- C++/C++11中std::string用法汇总
C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...
- C++-容器-string:数字to字符串【std::to_string()】、字符串to数字【std::stoi、stol、stoll、stoul、stoull、stof、stod、stold】
一.std 内置的数字转字符串函数 std::string to_string(int value); std::string to_string(long value); std::string t ...
- std::stoi stol stoul stoll stof stod
std::stoi stol stoul stoll stof stod 一系列函数为C++提供的转换string/wstring的方法,但是对于第二个参数网上说的是从第N个位置开始,其实并不是,不知 ...
- 【最贴心】C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)
先说结论: stoi: string型变量转换为int型变量 stol: string型变量转换为long型变量 stoul:string型变量转换为unsigned long型变量 stoll: s ...
- c++字符串转换为数字(stoi, stol, stoul, stoull, stof, stod, stold)
c++字符串转换为数字(stoi, stol, stoul, stoull, stof, stod, stold) 头文件#include <string> 1. stoi 将字符串转换成 ...
- C++字符串转换(stoi;stol;stoul;stoll;stoull;stof;stod;stold)
一.stoi 头文件string int stoi (const string& str, size_t* idx = 0, int base = 10); int stoi (const w ...
- C++学习心得:字符串转换(to_string,stoi,stol,stoul)
一.结论 使用C++11,并且导入头文件 #include<string> other ---> string to_string (int / long / float /...) ...
- 【C++标准库】std::string用法指南源码剖析
文章目录 1.ASCII码 (1)计算机如何表达字符 2.C 语言中的字符类型 char (1)思想:char 即整数 (3)C 语言帮手函数 (4)C语言中的字符串 (4)C 语言转义符 3.C++ ...
- C++stoi、stol、stoll 函数用法
stoi() 函数 #include <string> int stoi(const std::string& str, std::size_t* pos = 0, int bas ...
最新文章
- 传统的Linux中IPC通信原理
- UVA-10054 The Necklace (欧拉回路)
- superset出现A valid API access token is required to use Mapbox data
- 工作内外网同时连接方案
- iphone11边框喇手问题_别贪图小便宜!iPhone11真机上手,3个缺点不得不说
- Intel 64/x86_64/IA-32/x86处理器基本执行环境 (1) - 32位执行环境概述
- 常见面试题整理--Python概念篇
- 1049. 最后一块石头的重量 II(JavaScript)
- OpenCV基本线条操作
- 【Java必备技能二】防止表单重复提交方法
- 综合扫描 -- Ladon
- 配置库管理及版本管理规范
- linux常用软件收集
- 阿里云与华为USG防火墙IPSEC对接
- 2018年上半年综合素质作文
- Android 华为手机10.1 启动service Service starting has been prevented by iaware or trustsbase **
- 小米互联网思维新思考20141220
- ppt给图片增加高斯模糊_PPT图片处理小技巧
- openCV 自学笔记
- (JUC)图文并茂!!!! 超详细 偏向锁VS轻量级锁VS重量级锁VS自旋
热门文章
- java底层知识(6)--CPU、内存
- 如何查看mysql数据库链接情况_MySQL如何查看数据库连接数和状态
- 数据结构:堆的实现与建堆时间复杂度分析
- Andorid 11 数据和文件存储相关API
- “互联网+大数据”成为审讯突破口
- Failed to list units: No such method ‘ListUnitsFiltered‘ systemctl list-units
- python 画sigmoid、tanh、relu和ELU等激活函数
- JavaWeb_XML与Tomcat
- Java知识体系总结
- 纵观全局Struts1与Struts2的基本区别