59

What is the difference between atoi and stoi?

I know,

std::string my_string = "123456789";

In order to convert that string to an integer, you’d have to do the following:

const char* my_c_string = my_string.c_str(); 
int my_integer = atoi(my_c_string);

C++11 offers a succinct replacement:

std::string my_string = "123456789"; 
int my_integer = std::stoi(my_string);

1). Are there any other differences between the two?

2). Efficiency and performance wise which one is better?

3). Which is safer to use?

Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
  • 5
    You might want to read e.g. [this `std::stoi` reference](http://en.cppreference.com/w/cpp/string/basic_string/stol) and compare it to [this `std::atoi` reference](http://en.cppreference.com/w/cpp/string/byte/atoi). – Some programmer dude Dec 14 '13 at 13:36
  • 2
    Oh by the way, `std::atoi` is not the corresponding C function to `std::stoi`, that would instead by [`std::strtol`](http://en.cppreference.com/w/cpp/string/byte/strtol), which I recommend over `atoi`. – Some programmer dude Dec 14 '13 at 14:27
  • You are actually right on strtok point – Bhupesh Pant Dec 14 '13 at 19:52

1 Answers1

75

1). Are there any other differences between the two?

I find std::atoi() a horrible function: It returns zero on error. If you consider zero as a valid input, then you cannot tell whether there was an error during the conversion or the input was zero. That's just bad. See for example How do I tell if the c function atoi failed or if it was a string of zeros?

On the other hand, the corresponding C++ function will throw an exception on error. You can properly distinguish errors from zero as input.

2). Efficiency and performance wise which one is better?

If you don't care about correctness or you know for sure that you won't have zero as input or you consider that an error anyway, then, perhaps the C functions might be faster (probably due to the lack of exception handling). It depends on your compiler, your standard library implementation, your hardware, your input, etc. The best way is to measure it. However, I suspect that the difference, if any, is negligible.

If you need a fast (but ugly C-style) implementation, the most upvoted answer to the How to parse a string to an int in C++? question seems reasonable. However, I would not go with that implementation unless absolutely necessary (mainly because of having to mess with char* and \0 termination).

3). Which is safer to use?

See the first point.

In addition to that, if you need to work with char* and to watch out for \0 termination, you are more likely to make mistakes. std::string is much easier and safer to work with because it will take care of all these stuff.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Ali
  • 56,466
  • 29
  • 168
  • 265
  • 3
    This is very solid advise. In regards to you first point (return value vs error code), I checked the man pages for atoi and it said: `It is equivalent to: (int)strtol(str, (char **)NULL, 10);`, and from the man page for strtol, the second param: `(Thus, if *first_param is not `\0' but **second_param is `\0' on return, the entire string was valid.)`. So it seems like using strtol with the second param would be better than atoi to avoid the problem you describe in point 1. strtol can be included from stdlib.h. – Nick Desaulniers Apr 30 '15 at 19:19
  • @NickDesaulniers Yes, and if you check the question linked in my answer ("How to parse a string to an int in C++?"), you will find that the most upvoted answer also uses `strtol`. However, I would not go down that road unless I absolutely have to; I would not use `strtol` for the reasons I gave in my answer. – Ali Apr 30 '15 at 20:02
  • @NickDesaulniers Thanks for your comment by the way: It turned out that my answer had become outdated. The accepted answer is now different so my answer was referring to the wrong answer. – Ali Apr 30 '15 at 20:18
  • @Ali "On the other hand, the corresponding C++ function" - you mean just `atoi` not in the `std::` namespace? – David Doria Feb 04 '16 at 21:12
  • @DavidDoria I meant [std::stoi](http://en.cppreference.com/w/cpp/string/basic_string/stol). – Ali Feb 04 '16 at 21:43