【cppreference】C++11 stoi 文档阅读


int       stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
int       stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
(1)	(C++11)
long      stol( const std::string& str, std::size_t* pos = 0, int base = 10 );
long      stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
(2)	(C++11)
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
(3)	(C++11)
str	-	要
转换的字符串
pos	-	存储已处理字符数的整数的地址,pos如果不为空,则会返回一个字符串中遇到的第一个字符(非数字)的字符下标,最后一个base是默认10进制。
base	-	数的底
#include <iostream>
#include <string>
using namespace std;

#define debug(x) cout<<#x<<": "<<(x)<<endl;

bool test(string s,int base=0) {
	size_t pos = 3;
	cout << s << ": " << stoi(s,&pos, base) << endl;
	debug(pos)
	debug(s.substr(pos))
	return true;
}

int main() {

	// 若 base 为 ​0​ ,则自动检测数值进制
	test("0x1azzzz",0);
	test("017zzzz",0);
	test("017abcdefghijk",16);
	test("017zzzz",15);
	
	return 0;
}
0x1azzzz: 26
pos: 4
s.substr(pos): zzzz
017zzzz: 15
pos: 3
s.substr(pos): zzzz
017abcdefghijk: 397135343
pos: 9
s.substr(pos): ghijk
017zzzz: 22
pos: 3
s.substr(pos): zzzz

re:
https://www.cnblogs.com/weedboy/p/7160152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值