【发布时间】:2020-04-07 17:30:48
【问题描述】:
我想将字符串111的第一个数字添加到整数x = 0,使其等于
x = 0 + 1 = 1
以下代码采用字符1,而不是整数1:
int x = 0;
string str = "111";
x += str[1];
std::stoi 也不起作用:
x += std::stoi(str[1]);
【问题讨论】:
-
stoi不起作用,因为根据 cpp 引用,stoi将string作为参数,而不是char。str[1]是单个字符,而不是string。