Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between std::strtol and std::stoi?

Tags:

c++

c++11

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?

like image 360
jmstoker Avatar asked Oct 08 '13 22:10

jmstoker


1 Answers

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.

like image 52
Reed Copsey Avatar answered Oct 31 '22 17:10

Reed Copsey