For C++11 and later:
The go-to function for string-to-integer conversion is now stoi
, which takes a string
and returns an int
, or throws an exception on error.
No need for the verbose istringstream
hack mentioned in the accepted answer anymore.
(There's also stol
/stoll
/stof
/stod
/stold
for long
/long long
/float
/double
/long double
conversions, respectively.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…