I want to convert 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?
One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.
In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.
In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings.
In C++11 there are some nice new convert functions from std::string
to a number type.
So instead of
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
The possible options are described below:
1. 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. 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. 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. 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. 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. 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.
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