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

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. Cows and Cars UVA - 10491 (古典概率)

    按照题目的去推就好了 两种情况 1.第一次选择奶牛的门  概率是 a/(a+b) 打开c扇门后  除去选择的门 还剩 a-1-c+b扇门  则选到车的概率为b/(a-1-c+b) 2.第一次选择车的门 ...

  2. 三年java面试题

    前言: 楼主毕业三年,从大学时期就开始一直从事java web方面的开发.我在去年的今天有一篇帖子:两年java面试经验.经历了一年的上班,成长了很多.今年因为某些原因辞职了.从2月底辞职,到3月初, ...

  3. C++之基础知识20170830

    /*************************************************************************************************** ...

  4. 驱动之NandFlash的介绍与应用20170209

    本文主要介绍的是NAND FLASH的介绍与应用,直接看个人笔记即可:

  5. jetbrains phpstorm插件开发环境搭建

    2018.04.14 重要更新: 使用 gradle 进行构建可以免去下面大部分步骤,使用 gradle 我们仅需下载安装 JDK.Idea. 使用 gradle 的方法是,新建 Project,然后 ...

  6. advanced bash shell guide读书笔记

    http://note.youdao.com/noteshare?id=fc23a679849b4627d131d3ef07c74a71

  7. P3620 [APIO/CTSC 2007]数据备份

    P3620 [APIO/CTSC 2007]数据备份 题目描述 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同 ...

  8. git分支管理图

  9. Java的StringAPI的小练习

    //-------------String类-------------- //求两个字符串的最大相同子串 /* 思路: 1.找出较短的那个字符串 2.找出短串的所有子串,使用contains函数判断是 ...

  10. redis-cluster 集群搭建详细指南及常见问题集合

    只当个搬运工吧 搭建篇:https://www.cnblogs.com/mafly/p/redis_cluster.html  测试能用 常见问题: 1 redis操作key时出现以下错误 (erro ...