可以使用内置函数int()将string类型转换为整型。例如:
string s = "123"; int i = int(s);
也可以使用stoi()函数将string类型转换为整型。例如:
string s = "123"; int i = stoi(s);
如果string中包含非数字字符,则会抛出invalid_argument异常。
如果要将string转换为浮点型,可以使用float()或stof()函数。
例如: string s = "3.14"; float f = float(s);
或
string s = "3.14"; float f = stof(