我们希望使用sstream将字符串转换为整数。
但是我们不知道我们的字符串是否为整数,例如它可能是“hello 200”,而我们想要其中的200,或者它可能是“hello”,那么就没有解决方案!
当字符串中只有一个整数时,我有以下代码:
现在,如果s =“hello 200!”或s =“hello”,我们该怎么做呢?
但是我们不知道我们的字符串是否为整数,例如它可能是“hello 200”,而我们想要其中的200,或者它可能是“hello”,那么就没有解决方案!
当字符串中只有一个整数时,我有以下代码:
inline int string_to_int(string s)
{
stringstream ss(s);
int x;
ss >> x;
return x;
}
现在,如果s =“hello 200!”或s =“hello”,我们该怎么做呢?