std::stoi C++ · STL
字符串转整数函数,把 std::string 的前缀整数部分转换成 int,并可通过位置参数返回停止位置。
怎么用
int n = std::stoi(字符串);
小例子
int n = std::stoi("42");
std::cout << n;
运行结果
42
提醒:转换失败抛 std::invalid_argument,超出范围抛 std::out_of_range;遇到第一个无法转换的字符就停止,返回已转换部分;需要严格校验请检查位置参数或自行判断
官方文档:https://en.cppreference.com/w/cpp/string/basic_string/stol
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C++全部词条