c++ 11字符串与string转换常用函数
这里主要介绍一下string to int 其他方法与这个类似,可到头文件 <string> 中查看
@_Str 转换的字符串
@_Idx 转换的长度(位数)
@_Base 进制
double stod(const string& _Str, size_t *_Idx = nullptr);
float stof(const string& _Str, size_t *_Idx = nullptr);
int stoi(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
long stol(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
long double stold(const string& _Str, size_t *_Idx = nullptr);
unsigned long stoul(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
long long stoll(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
unsigned long long stoull(const string& _Str, size_t *_Idx = nullptr, int _Base = 10);
std::string y = "253647586946334221002101219955219971002";
int x; try {
x = stoi(y);
}
catch (std::invalid_argument&){
// if no conversion could be performed
cout << "Invalid_argument" << endl;
}
catch (std::out_of_range&){
// if the converted value would fall out of the range of the result type
// or if the underlying function (std::strtol or std::strtoull) sets errno
// to ERANGE.
cout << "Out of range" << endl;
}
catch (...) {
// everything else
cout << "Something else" << endl;
}
return 0;
string to_string(int _Val);
string to_string(unsigned int _Val);
string to_string(long _Val); string
to_string(unsigned long _Val);
string to_string(long long _Val);
string to_string(unsigned long long _Val);
string to_string(float _Val);
string to_string(double _Val);
string to_string(long double _Val);
可以看到 to_string(); 方法不仅支持各种整型( 有符号的、无符号的、长的、短的 任意组合 ),还支持各种浮点型( float/double 包括长的、短的 )
c++ 11字符串与string转换常用函数的更多相关文章
- c++中string的常用函数说明
string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...
- C++string类常用函数
C++string类常用函数 string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初 ...
- Delphi字符串的基本操作与常用函数
参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...
- Kotlin——初级篇(八):关于字符串(String)常用操作汇总
在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin--初级篇(三):数据类型详解这篇文章. 在 ...
- Kotlin——关于字符串(String)常用操作汇总
在前面讲解Kotlin数据类型的时候,提到了字符串类型,当然关于其定义在前面的章节中已经讲解过了.对Kotlin中的数据类型不清楚的同学.请参考Kotlin——初级篇(三):数据类型详解这篇文章. 在 ...
- C string.h 常用函数
参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...
- db2字符串截取方法及常用函数
select substr(index_code, 1, locate('-', index_code)-1) from report_data substr(str,m,n)表示从str中的m个字符 ...
- C语言字符,字符串,字节操作常用函数
strlen 这个函数是在 string.h 的头文件中定义的 它的函数原型是 size_t strlen( const char ); size_t 是一个无符号整型,是这样定义的 typedef ...
- JS中String,Math常用函数
String对象: 1.length属性 说明:获取字符串的长度 实例: var str="abc"; var i=str.length;//output:3 2.charAt() ...
随机推荐
- MeteoInfoLab脚本示例:读取文本文件绘制散度图
MeteoInfoLab中读取文本文件数据的函数是asciiread,获取文本文件行.列数的函数是numasciirow和numasciicol,和NCL中函数名一致,但都是小写字母.本例中的示例数据 ...
- day10 Pyhton学习
一.昨日内容回顾 函数: 定义:对功能或者动作的封装 def 函数名(形参): 函数体 函数名(实参) return: 返回,当程序运行到return的时候,终止函数的执行 一个函数一定拥有返回值 ...
- laravel或者lumen门面和服务提供者使用
关于laravel门面和服务提供者使用的一点见解,门面之词,不足之处,还请多多指教. 在laravel中,我们可能需要用到自己添加的类时,可以建立一个文件夹专门存放类文件,也可以使用lara ...
- flutter——android报错建议Suggestion: add 'tools:replace="android:label"'
问题: 安装了一个新包,android出现了报错,建议add 'tools:replace="android:label"'. 原因: 项目application的label属性冲 ...
- count=count++
count=count++ 的最终值是count =count,没加上1,该是啥是啥 count++ 的最终值是加一的值 注意: count = count ++ + 1 最终还是1
- golang开发:http请求redirect的问题
这两天在开发项目的时候遇到了一个问题,请求了一个URL,它会302到另一个地址,本意上只是想检查这个URL是否会做3XX的redirect跳转,结果每次reqeust都会返回最后一跳的结果.后来就看了 ...
- image restoration(IR) task
一般的,image restoration(IR)任务旨在从观察的退化变量$y$(退化模型,如式子1)中,恢复潜在的干净图像$x$ $y \text{} =\text{}\textbf{H}x\tex ...
- 在CentOS 8 上 部署 .Net Core 应用程序
在Centos 8 上 部署 .Net Core 应用程序 -- 记录篇 1.更新dnf 源 1 dnf update 2.安装 Asp.Net Core 运行时 1 dnf install ...
- 操作安装docker
在本地建造起vue-cli服务 参考项目:https : //gitee.com/QiHanXiBei/myvue 在本地建造起一个django项目架构,通过/ hello能够打印出helloworl ...
- B. Two Fairs 解析(思維、DFS、組合)
Codeforce 1276 B. Two Fairs 解析(思維.DFS.組合) 今天我們來看看CF1276B 題目連結 題目 給一個連通圖,並給兩個點(\(a,b\)),求有多少點對使得:任一路徑 ...