## How can I convert a std::string to int?

### Question

Just have a quick question. I've looked around the internet quite a bit and I've found a few solutions but none of them have worked yet. Looking at converting a string to an int and I don't mean ASCII codes.

For a quick run-down, we are passed in an equation as a string. We are to break it down, format it correctly and solve the linear equations. Now, in saying that, I'm not able to convert a string to an int.

I know that the string will be in either the format (-5) or (25) etc. so it's definitely an int. But how do we extract that from a string?

One way I was thinking is running a for/while loop through the string, check for a digit, extract all the digits after that and then look to see if there was a leading '-', if there is, multiply the int by -1.

It seems a bit over complicated for such a small problem though. Any ideas?

2018/09/26
1
512
9/26/2018 8:55:09 AM

In C++11 there are some nice new convert functions from `std::string` to a number type.

``````atoi( str.c_str() )
``````

you can use

``````std::stoi( str )
``````

where `str` is your number as `std::string`.

There are version for all flavours of numbers: `long stol(string)`, `float stof(string)`, `double stod(string)`,... see http://en.cppreference.com/w/cpp/string/basic_string/stol

2014/11/20
765
11/20/2014 8:53:55 AM

``````std::istringstream ss(thestring);
ss >> thevalue;
``````

To be fully correct you'll want to check the error flags.

2011/10/05

The possible options are described below:

1. First option: sscanf()

``````    #include <cstdio>
#include <string>

int i;
float f;
double d;
std::string str;

// string -> integer
if(sscanf(str.c_str(), "%d", &i) != 1)
// error management

// string -> float
if(sscanf(str.c_str(), "%f", &f) != 1)
// error management

// string -> double
if(sscanf(str.c_str(), "%lf", &d) != 1)
// error management
``````

This is an error (also shown by cppcheck) because "scanf without field width limits can crash with huge input data on some versions of libc" (see here, and here).

2. Second option: std::sto*()

``````    #include <iostream>
#include <string>

int i;
float f;
double d;
std::string str;

try {
// string -> integer
int i = std::stoi(str);

// string -> float
float f = std::stof(str);

// string -> double
double d = std::stod(str);
} catch (...) {
// error management
}
``````

This solution is short and elegant, but it is available only on on C++11 compliant compilers.

3. Third option: sstreams

``````    #include <string>
#include <sstream>

int i;
float f;
double d;
std::string str;

// string -> integer
std::istringstream ( str ) >> i;

// string -> float
std::istringstream ( str ) >> f;

// string -> double
std::istringstream ( str ) >> d;

// error management ??
``````

However, with this solution is hard to distinguish between bad input (see here).

4. Fourth option: Boost's lexical_cast

``````    #include <boost/lexical_cast.hpp>
#include <string>

std::string str;

try {
int i = boost::lexical_cast<int>( str.c_str());
float f = boost::lexical_cast<int>( str.c_str());
double d = boost::lexical_cast<int>( str.c_str());
} catch( boost::bad_lexical_cast const& ) {
// Error management
}
``````

However, this is just a wrapper of `sstream`, and the documentation suggests to use `sstream` for better error management (see here).

5. Fifth option: strto*()

This solution is very long, due to error management, and it is described here. Since no function returns a plain int, a conversion is needed in case of integer (see here for how this conversion can be achieved).

6. Sixth option: Qt

``````    #include <QString>
#include <string>

bool ok;
std::string;

int i = QString::fromStdString(str).toInt(&ok);
if (!ok)
// Error management

float f = QString::fromStdString(str).toFloat(&ok);
if (!ok)
// Error management

double d = QString::fromStdString(str).toDouble(&ok);
if (!ok)
// Error management
``````

Conclusions

Summing up, the best solution is C++11 `std::stoi()` or, as a second option, the use of Qt libraries. All other solutions are discouraged or buggy.

2020/03/13

use the atoi function to convert the string to an integer:

``````string a = "25";

int b = atoi(a.c_str());
``````

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

2014/03/30

Here is their example:

The following example treats command line arguments as a sequence of numeric data:

``````int main(int argc, char * argv[])
{
using boost::lexical_cast;

std::vector<short> args;

while(*++argv)
{
try
{
args.push_back(lexical_cast<short>(*argv));
}
{
args.push_back(0);
}
}
...
}
``````
2017/09/26

Admittedly, my solution wouldn't work for negative integers, but it will extract all positive integers from input text containing integers. It makes use of `numeric_only` locale:

``````int main() {
int num;
std::cin.imbue(std::locale(std::locale(), new numeric_only()));
while ( std::cin >> num)
std::cout << num << std::endl;
return 0;
}
``````

Input text:

`````` the format (-5) or (25) etc... some text.. and then.. 7987...78hjh.hhjg9878
``````

Output integers:

`````` 5
25
7987
78
9878
``````

The class `numeric_only` is defined as:

``````struct numeric_only: std::ctype<char>
{
numeric_only(): std::ctype<char>(get_table()) {}

{
rc(std::ctype<char>::table_size,std::ctype_base::space);

std::fill(&rc['0'], &rc[':'], std::ctype_base::digit);
return &rc;
}
};
``````

Complete online demo : http://ideone.com/dRWSj

2011/10/05