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 ...
随机推荐
- 病毒侵袭 HDU - 2896(ac自动机 板题)
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~ 但网路上总有那么些网站,开 ...
- 快速傅里叶变换FFT / NTT
目录 FFT 系数表示法 点值表示法 复数 DFT(离散傅里叶变换) 单位根的性质 FFT(快速傅里叶变换) IFFT(快速傅里叶逆变换) NTT 阶 原根 扩展知识 FFT 参考blog: 十分简明 ...
- 【BZOJ3504】危桥(网络流)
[BZOJ3504]危桥(网络流) 题面 BZOJ 洛谷 Description Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双 向的, ...
- 最小生成树-----Prim算法与Kruskal算法(未完
生成树(spanning tree):无向联通图的某个子图中,任意两个顶点互相都联通并且形成了一棵树,那么这棵树就叫做生成树. 最小生成树(MST,minimum spanning tree):如果为 ...
- 【DP】【CF9D】 How many trees?
传送门 Description 给你两个正整数\(n,h\),求由\(n\)个点组成的高度大于等于\(h\)的二叉树有多少个 Input 一行两个整数\(n,h\) Output 一个整数代表答案. ...
- 关于OpenCV的stitching使用
配置环境:VS2010+OpenCV2.4.9 为了使用OpenCV实现图像拼接头痛了好长时间,一直都没时间做,今天下定决心去实现基本的图像拼接. 首先,看一看使用OpenCV进行拼接的方法 基本都是 ...
- Cyrus SASL介绍(翻译)
http://blog.sina.com.cn/s/blog_7695e9f40100pnpa.html Cyrus SASL介绍 1. 综述 这篇文档讲述的是系统管理员配置SASL的方法,其中详细的 ...
- Vue.js随笔三(npm init webpack my-project指令安装失败解决方案)
如果没有安装淘宝给的镜像就先安装一下,指令如下,对!就是如此简单: npm install -g cnpm -registry=https://registry.npm.taobao.org 首先输入 ...
- web启动@Autowired不能自动注入
使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...
- 新手应知道的ASP.NET代码编写规范
1.局部变量的名称要有意义,尽量用对应的英文命名,比如“用户姓名”变量,不要用aa bb cc等来命名,而要使用userName. 2.不要使用单个字母的变量,如i.n.x等.而要使用index.te ...