Is there a function that could replace atoi in c++. I made some research and didn't find anything to replace it, the only solutions would be using cstdlib or implementing it myself
-
6Why would you need to replace it? – Jesus Ramos Aug 13 '11 at 13:32
-
I don't want to use c stuff in c++ code – Mansuro Aug 13 '11 at 13:35
-
is it really that hard to type atoi(str.c_str()); ? Also kind of hard to avoid that considering C++ is written in C so.... yeah – Jesus Ramos Aug 13 '11 at 13:36
-
well, it is advised to not use c code in c++ – Mansuro Aug 13 '11 at 13:41
-
As you can see you can use boost, problem is now you have just added the boost library to your project to use 1 method. Which is worse using a C function or including an entire library for 1 function? – Jesus Ramos Aug 13 '11 at 13:42
-
4@Jesus: `atoi` is not safe! It doesn't handle invalid input. Also, he can use a lot more other utilities from boost. – Nawaz Aug 13 '11 at 13:46
-
I guess it would be better to use atoi, but still... – Mansuro Aug 13 '11 at 13:46
-
@Nawaz, well if your program is getting invalid input I'm hoping that you're fixing that already. You shouldn't rely on functions to do a lot of things that they were never designed to do. – Jesus Ramos Aug 13 '11 at 13:48
-
3The C++ standard library explicitly contains the C standard library, so `
` is an entirely legitimate part of C++. Say `std::atoi` if that makes you feel better :-) – Kerrek SB Aug 13 '11 at 13:48 -
1@Jesus: That is what I'm saying `atoi` doesn't handle invalid input. – Nawaz Aug 13 '11 at 13:52
-
@Kerrek: Just because C++ Standard has included `
`, it doesn't mean that `std::atoi` is safe (if you imply so in your comment). Stdlib also includes ` – Nawaz Aug 13 '11 at 13:56`, but it doesn't mean that one should use `std::scanf` and `std::prinf` instead of `std::cin` and `std::cout`. -
@Nawaz, I realize this but you're trying to say this as if it's a bad thing when in reality if you claim that it not handling all input is a bad thing you're just blaming your code problem on something else since you want the function to do all the work for you. In this case all ints are valid therefore no valid error can be returned easily by this function. – Jesus Ramos Aug 13 '11 at 13:57
-
@Jesus: What I'm trying to say is that `boost::lexical_cast` is doing the input-check and conversion both. If you use `atoi`, then you've to write code what `boost::lexical_cast` is already doing. Also, if you want to write code to check if the string is valid input for `atoi` or not, then why not convert it youself as well? Why use `atoi` at all? – Nawaz Aug 13 '11 at 14:01
-
16I agree with @Nawaz in that `atoi` has a very poor interface. `strtol` is suitable replacement, not because it does anything different - it doesn't, but because it has an interface that allows you to check whether a successful conversion happened. I don't really understand @JesusRamos argument that you should write more code to check your input before passing it to a conversion function. Isn't writing a function that determines whether a string can be parsed into a number just as hard as writing a function that does that conversion? In which case, why not use a library function that does both. – CB Bailey Aug 13 '11 at 14:59
-
2@Jesus Ramos: The task of "verifying" the `int` before passing it to `atoi` is exactly as complex as `atoi` itself. In fact, while it is possible to pre-verify the *syntax* of the input, it is not meanigfully possible to catch *overflow* without doing the actual conversion. For this reason `atoi` is a "dead" function, that has no uses in actual code. Virtually any code that uses `atoi` is broken for that reason alone. String-to-integer conversions in C language has always been performed exclusively by `strto...` functions. They have no alternatives in C standard library. – AnT stands with Russia Aug 27 '13 at 00:00
-
@Mansuro: ...In other words, the very original question is already quite misguided. You shouldn't have been lookig for "equivalent of `atoi`" since you shouldn't have been using `atoi` in the first place. Ever. You need equivalent of `strto...` functions. Although these functions are available in C++ as well, a better option might be `lexical_cast`. – AnT stands with Russia Aug 27 '13 at 00:19
5 Answers
If you don't want to use Boost, C++11 added std::stoi
for strings. Similar methods exist for all types.
std::string s = "123"
int num = std::stoi(s);
Unlike atoi
, if no conversion can be made, an invalid_argument
exception is thrown. Also, if the value is out of range for an int, an out_of_range
exception is thrown.

- 845
- 1
- 7
- 26

- 6,637
- 4
- 45
- 60
-
1I keep expecting there to be `to_int` and `to_long` methods on std::string, or something, but I guess this is almost as good. – Steve Summit Dec 21 '15 at 23:32
boost::lexical_cast
is your friend
#include <string>
#include <boost/lexical_cast.hpp>
int main()
{
std::string s = "123";
try
{
int i = boost::lexical_cast<int>(s); //i == 123
}
catch(const boost::bad_lexical_cast&)
{
//incorrect format
}
}

- 130,161
- 59
- 324
- 434
-
2Armen: `boost::lexical_cast` may throw, so surround it with try-catch to make it more complete. – Nawaz Aug 13 '11 at 13:37
-
1@Nawaz: I have given a reference to the documentation. In this case the try-catch is not needed. But OK, I will add it. – Armen Tsirunyan Aug 13 '11 at 13:40
You can use the Boost function boost::lexical_cast<> as follows:
char* numericString = "911";
int num = boost::lexical_cast<int>( numericString );
More information can be found here (latest Boost version 1.47). Remember to handle exceptions appropriately.
Without boost:
stringstream ss(my_string_with_a_number); int my_res; ss >> my_res;
About as annoying as the boost version but without the added dependency. Could possibly waste more ram.

- 7,924
- 1
- 20
- 18
-
2
-
1That depends on if you like the direction C++ is taking or not :) But let's not pollute this question. – Torp Aug 13 '11 at 14:00
-
1@Torp: try converting `std::string s="8978x9"`. Your approach will fail without notifying you. On the other hand, `boost::lexical_cast` will throw exception and you will know it! And boost is not annoying as @Armen already said. – Nawaz Aug 13 '11 at 14:04
-
@Torp, what do you mean by "if you like the direction C++ is taking" is it the bad or the good direction? – Mansuro Aug 13 '11 at 14:30
-
1In my opinion it's the 'unmanageably complex' direction :) But sadly i don't know of a better general purpose alternative. – Torp Aug 14 '11 at 09:29
-
1To address @Nawaz's concern of possible failure with this method, you can test the stringstream after attempting to extract something else: `char x; if (!(ss >> x)) /*success*/` – kapace Feb 13 '13 at 05:12
-
@kapace: As I said, if the input is `"8978x9"` for example, then `ss` will read only `8978`, without telling you the problem that the string contains `'x'` which is an invalid digit. See this [Online Demo](http://stacked-crooked.com/view?id=3c51185db73e459d33c954e03c07814c) – Nawaz Feb 13 '13 at 06:23
-
@Nawaz: Hmm, this is what I meant: http://stacked-crooked.com/view?id=9d05614040a9d2810018dc2bcc8a107f-18aa934a8d82d638dde2147aa94cac94 – kapace Mar 03 '13 at 01:51
-
1
You don't say why atoi
is unsuitable so I am going to guess it has something to do with performance. Anyway, clarification would be helpful.
Using Boost Spirit.Qi is about an order of magnitude faster than atoi
, at least in tests done by Alex Ott.
I don't have a reference but the last time I tested it, Boost lexical_cast
was about an order of magnitude slower than atoi
. I think the reason is that it constructs a stringstream, which is quite expensive.
Update: Some more recent tests

- 4,475
- 1
- 26
- 23
-
4Presumably `atoi` is unsuitable because of its behavior given an invalid argument (I believe the behavior is undefined). – Keith Thompson Aug 26 '13 at 23:15
-
MISRA rules prohibit atoi because of the undefined behavior whem an invalid conversion is processed. – Larry_C Nov 26 '21 at 16:30