栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ stoi 介绍

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ stoi 介绍

1、 功能

int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);

将str 的内容 解析为一个 特定base的int数值。

2、参数 1. str

一个表示整数的string类型的对象

2. idx
  1. 如果为null,代表不使用这个参数
  2. 指向size_t类型对象的指针,该函数将其值设置为str中数值后面的下一个字符的位置,也就是str中既包含数字又包含非数字的话,那么该函数会将数值后面的第一个字符的位置赋值给这个指针。
3. base

确定以何种的基数去解释str中的数值。默认为10。如果填0的话,就按照str中的标志去判定(如0x)

3、例子
// stoi example
#include    // std::cout
#include      // std::string, std::stoi
using namespace std;
int main ()
{
  string dec = "2022, a new year";
  string hex = "403c";
  string bin = "-101010";
  string autoConvert = "0x6f";
  
  string::size_type sz;

  int i_dec = stoi(dec,&sz);
  int i_hex = stoi(hex,nullptr,16);
  int i_bin = stoi (bin,nullptr,2);
  int i_auto = stoi (autoConvert,nullptr,0);

  cout << dec << ": " << i_dec << "   n sz point to:" << dec[sz] << 'n';
  cout << hex << ": " << i_hex << 'n';
  cout << bin << ": " << i_bin << 'n';
  cout << autoConvert << ": " << i_auto << 'n';

  return 0;
}
输出:
2022, a new year: 2022   
sz point to:,
403c: 16444
-101010: -42
0x6f: 111
参考资料

https://www.cplusplus.com/reference/string/stoi/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/691132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号