Disclaimer: Links are to cppreference.com
So I have known for a while that std::atoi has been deprecated and it's been recommended to use std::strtol instead.
C++11 has introduced std::stoi and I'm trying to understand why one would choose to use it over std::strtol
.
From what I understand is that stoi calls strtol but throws exceptions. Also it returns an integer instead of a long.
Are these the main differences, what am I missing?
Are these the main differences, what am I missing?
The newer, std::stoi
also works directly from std::string
(so you don't have to litter your code with .c_str()
calls) and optionally provides you the first unmatched character as an index via a size_t
, rather than as a pointer.
These changes simplify the usage from within your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With