C++——stoi函数
版权声明:本文系原创,转载请声明出处。
1. 函数原型
int stoi (const string& str, size_t* idx = , int base = );
int stoi (const wstring& str, size_t* idx = , int base = );
2. 参数说明
- str
- String object with the representation of an integral number.
- idx
- Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
- base
- Numerical base (radix) that determines the valid characters and their interpretation.
If this is 0, the base used is determined by the format in the sequence (see strtol for details). Notice that by default this argument is 10, not 0.
3. 返回值
如果解析成功,返回转换后的整数。
On success, the function returns the converted integral number as an int value.
4. 异常处理
stoi当字符串不符合规范时,会抛出异常,所以在使用stoi时应该有必要的异常处理:
#include <stdexcept>
#include <iostream>
#include <string>
using namespace std; int main()
{
std::string y = "";
int x; try {
x = stoi(y);
}
catch (std::invalid_argument&){
// If no conversion could be performed, an invalid_argument exception is thrown.
cout << "Invalid_argument" << endl;
}
catch (std::out_of_range&){
// If the value read is out of the range of representable values by an int, an out_of_range exception is thrown.
cout << "Out of range" << endl;
}
catch (...) {
// everything else
cout << "Something else" << endl;
}
return ;
}
参考资料:
- https://blog.csdn.net/u014694994/article/details/79074566
- http://www.cplusplus.com/reference/string/stoi/?kw=stoi
C++——stoi函数的更多相关文章
- atoi()和stoi()函数
C++的字符处理函数,把数字字符串转换成int输出 头文件都是#include<cstring> atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_ ...
- 对称平方数(to_string函数,stoi函数真香)
题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 输入描述: 无 输出描述: 每行一个数,表示对称平方数. 示例1 输入 复制 无 输出 复制 无解题思 ...
- C++中stoi函数
作用: 将 n 进制的字符串转化为十进制 头文件: #include <string> 用法: stoi(字符串,起始位置,n进制),将 n 进制的字符串转化为十进制 示例: stoi(s ...
- 【C++】atoi与stoi
stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则显示-214 ...
- atoi和stoi
vs环境下:stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则 ...
- [LeetCode] Solve the Equation 解方程
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- C/C++中字符串和数字互转小结
一. 数字 转 char*型 1.sprintf函数(适合C和C++) 示例: char str[50]; int num = 345; sprintf(str,"%d",num) ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- leetCode题解之旋转数字
1.题目描述 X is a good number if after rotating each digit individually by 180 degrees, we get a valid n ...
随机推荐
- AtCoder Regular Contest 086 E - Smuggling Marbles(树形迭屁)
好强的题. 方案不好算,改成算概率,注意因为是模意义下的概率所以直接乘法逆元就好不要傻傻地开double. 设$f[i][d][0]$为第i个节点离d层的球球走到第i个点时第i个点没有球的概率, $f ...
- 解题:USACO06DEC Milk Patterns
题面 初见SA 用了一个常见的按$height$分组的操作:二分答案,然后按$height$分组,遇到一个$height$小于$mid$的就丢进下一组并更新答案,如果最多的那组不少于$k$个说明可行 ...
- NOIP2010-2015后四题汇总
1.前言 正式开始的第一周的任务——把NOIP2010至NOIP2015的所有D1/2的T2/3写出暴力.共22题. 暴力顾名思义,用简单粗暴的方式解题,不以正常的思路思考.能够较好的保证正确性,但是 ...
- 相机标定 和 单应性矩阵H
求解相机参数的过程就称之为相机标定. 1.相机模型中的四个平面坐标系: 1.1图像像素坐标系(u,v) 以像素为单位,是以图像的左上方为原点的图像坐标系: 1.2图像物理坐标系(也叫像平面坐标系)(x ...
- Codeforces 937.D Sleepy Game
D. Sleepy Game time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- javascript基本介绍
javascript是一种广泛用于客户端web开发的脚本语言,常采用来给html网页添加动态功能,比如响应客户的各种操作. 脚本语言是什么? (1).脚本语言往往不能独立运行,它和html/jsp/p ...
- Git之git push不手动输入用户名和密码
每次git push时都要输入用户名和密码,感觉很啰嗦,总结了网上的解决办法,有的发现不可以(原因未知),记录一个对我自己可用的方式,我的是windows. 1:添加环境变量 2:在%HOME%目录下 ...
- 写文章 使用conda管理python环境
使用conda管理python环境
- NOIP模拟赛16
NOIP2017金秋冲刺训练营杯联赛模拟大奖赛第一轮Day2 期望得分:100+100+ =200+ 实际得分:100+40+70=210 T1天天寄快递 直接模拟,代码丢了...... T2天天和不 ...
- HDU 6205 尺取
容易看出来,扩增一倍,找最长的区间就行了 /** @Date : 2017-09-11 12:43:11 * @FileName: 1012.cpp * @Platform: Windows * @A ...