菜鸟笔记
提升您的技术认知

C++11中stoi函数的异常处理

stoi当字符串不符合规范时,会抛出异常,所以你应该捕获异常来做。

#include <stdexcept>
#include <iostream>
#include <string>
using namespace std;

int main()
{
	std::string y = "253647586946334221002101219955219971002";
	int x;

	try {
		x = stoi(y);
	}
	catch (std::invalid_argument&){
		// if no conversion could be performed
		cout << "Invalid_argument" << endl;
	}
	catch (std::out_of_range&){
		// if the converted value would fall out of the range of the result type 
		// or if the underlying function (std::strtol or std::strtoull) sets errno 
		// to ERANGE.
		cout << "Out of range" << endl;
	}
	catch (...) {
		// everything else
		cout << "Something else" << endl;
	}
	return 0;
}