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

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. jQuery多重事件绑定

    1. <a> a标签默认绑定了一个onclick事件,当自己再写一个onclick事件的时候,默认自己写的那个优先执行. 如下程序,先执行(123),然后再发生跳转. <!DOCTY ...

  2. 洛谷 P3373 【模板】线段树 2 解题报告

    P3373 [模板]线段树 2 题目描述 如题,已知一个数列,你需要进行下面三种操作: 1.将某区间每一个数乘上\(x\) 2.将某区间每一个数加上\(x\) 3.求出某区间每一个数的和 输入输出格式 ...

  3. SQL中的替换函数replace()使用

    语法REPLACE ( string_expression , string_pattern , string_replacement ) 参数string_expression 要搜索的字符串表达式 ...

  4. spark(三)spark sql

    一.DataFrame 1.DataFrame是组织成命名列的数据的分布式集合,类似于关系型数据库的一张表,如果没有列名就等于RDD,如果有列名,就是DataFrames DataFrames可以从各 ...

  5. Error: Chromium revision is not downloaded. Failed to download Chromium

    在使用prerender-spa-plugin做前端预渲染的时候,安装puppeteer的时候因为下载Chromium 失败报错,有如下解决方法: 1.使用Chromium 国内源 npm confi ...

  6. Linux HugePage 特性

    HugePage,就是指的大页内存管理方式.与传统的4kb的普通页管理方式相比,HugePage为管理大内存(8GB以上)更为高效.本文描述了什么是HugePage,以及HugePage的一些特性. ...

  7. python学习(十一)测试和调试

    最近学习了python的错误处理和几种测试方法 1 try except 可以通过try except方式捕捉异常 try: print('try...') r = 10/0 print('resul ...

  8. Qt ------ stylesheet 样式

    1.所有的窗口组件都可以用 setStyleSheet() 设置样式 2.使用样式,显示效果可以不受平台影响,比如保证window 7 和 linux 显示效果是一样的 QVariant 如果 sty ...

  9. 「HTML5」url、href、src区别

    一.URL的概念 统一资源定位符(或称统一资源定位器/定位地址.URL地址等,英语:Uniform Resource Locator,常缩写为URL),有时也被俗称为网页地址(网址).如同在网络上的门 ...

  10. mysql \G

    mysql 命令区分大小写.ego       (\G) Send command to mysql server, display result vertically. go        (\g) ...