版权声明:本文系原创,转载请声明出处。

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函数的更多相关文章

  1. atoi()和stoi()函数

    C++的字符处理函数,把数字字符串转换成int输出 头文件都是#include<cstring> atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_ ...

  2. 对称平方数(to_string函数,stoi函数真香)

    题目描述 打印所有不超过n(n<256)的,其平方具有对称性质的数.如11*11=121. 输入描述: 无 输出描述: 每行一个数,表示对称平方数. 示例1 输入 复制 无 输出 复制 无解题思 ...

  3. C++中stoi函数

    作用: 将 n 进制的字符串转化为十进制 头文件: #include <string> 用法: stoi(字符串,起始位置,n进制),将 n 进制的字符串转化为十进制 示例: stoi(s ...

  4. 【C++】atoi与stoi

    stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则显示-214 ...

  5. atoi和stoi

    vs环境下:stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则 ...

  6. [LeetCode] Solve the Equation 解方程

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  7. C/C++中字符串和数字互转小结

    一. 数字 转 char*型 1.sprintf函数(适合C和C++) 示例: char str[50]; int num = 345; sprintf(str,"%d",num) ...

  8. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  9. leetCode题解之旋转数字

    1.题目描述 X is a good number if after rotating each digit individually by 180 degrees, we get a valid n ...

随机推荐

  1. P3469 [POI2008]BLO-Blockade

    题意翻译 在Byteotia有n个城镇. 一些城镇之间由无向边连接. 在城镇外没有十字路口,尽管可能有桥,隧道或者高架公路(反正不考虑这些).每两个城镇之间至多只有一条直接连接的道路.人们可以从任意一 ...

  2. (转)修改Android 的framework层后,重新编译

    1.下面方法适合真机:下载android源码,然后编译你修改的framwork的代码,会生成framework.jar,然后push到system/framework目录下,重启机器!ok 2,下面方 ...

  3. 【BZOJ2763】飞行路线(最短路)

    [BZOJ2763]飞行路线(最短路) 题面 BZOJ Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标 ...

  4. BZOJ3110 K大数查询 【线段树 + 整体二分 或 树套树(非正解)】

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  5. codeforces765F Souvenirs

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. 【期望】【P5081】Tweetuzki 爱取球

    Description Tweetuzki 有一个袋子,袋子中有 \(N\) 个无差别的球.Tweetuzki 每次随机取出一个球后放回.求取遍所有球的期望次数. 取遍是指,袋子中所有球都被取出来过至 ...

  7. ORACLE获取某个时间段之间的月份列表

    返回1-31,或者1-12,或者某个 select rownum   from dual   connect by rownum<31 就是connect by http://marcospri ...

  8. omnet++4.0安装使用

    http://my.oschina.net/u/2269841/blog/423659 本文主要借鉴该文章在此对作者表示由衷的感谢http://blog.csdn.net/xiaobei4929/ar ...

  9. 三元组ADT (数据结构C语言版) C++实现

    很久没用C语言,都忘了C语言中没有引用参数,下面的代码中用到了C语言没有的引用参数. 首先是一些表示状态的全局变量 common.h #define TRUE 1 #define FALSE 0 #d ...

  10. [ldap]slapcat/ldapsearch与ldap备份

    http://serverfault.com/questions/577356/ldap-backup-with-slapcat-vs-ldapsearch Used: openldap-server ...