This post will discuss how to convert a string to int in C++.
1. Using std::stoi
function
The standard approach is to use the std::stoi
function for converting a string to an integer. The stoi
function was introduced in C++11 and is defined in the header <string>
. It throws std::invalid_argument
or std::out_of_range
exception on a bad input or integer overflow, respectively. It is worth noting that it will convert the strings like 10xyz
to integer 10
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s = "10"; try { int i = std::stoi(s); std::cout << i << std::endl; } catch (std::invalid_argument const &e) { std::cout << "Bad input: std::invalid_argument thrown" << std::endl; } catch (std::out_of_range const &e) { std::cout << "Integer overflow: std::out_of_range thrown" << std::endl; } return 0; } |
2. Using string streams
std::stringstream
can be used to convert std::string
to other data types and vice versa. This suffers from the same problem as std::stoi
did, i.e., it will convert the strings like 10xyz
to integer 10
. It returns INT_MAX
or INT_MIN
if the converted value is out of the range of integer data type. If the value of the string can’t be represented as an int, 0 is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <sstream> int main() { std::string s = "10"; int i; std::istringstream(s) >> i; std::cout << i << std::endl; return 0; } |
3. Using boost::lexical_cast
Another good alternative is to use the boost::lexical_cast. It throws the boost::bad_lexical_cast
exception when no valid integer could be constructed, or overflow happens.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <boost/lexical_cast.hpp> #include <string> int main() { std::string s = "10"; int i; try { i = boost::lexical_cast<int>(s); std::cout << i << std::endl; } catch (boost::bad_lexical_cast const &e) { // bad input std::cout << "error" << std::endl; } return 0; } |
4. Using std::atoi
function
We can also use the std::atoi
function to convert a string to an int. It takes a C-string as a parameter, so we need to convert our string to a C-string. We can do that using std::string::c_str
.
Please note that its behavior is undefined if the converted value is out of the range of integer data type. If the value of the string can’t be represented as an int, 0 is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <string> #include <cstdlib> int main() { std::string s = "10"; int i = std::atoi(s.c_str()); std::cout << i << std::endl; return 0; } |
5. Using sscanf()
function
We can pass a C-string to the sscanf()
function as a parameter and convert it into the corresponding data type specified in the format specifier. It would return the total number of arguments on success and EOF on failure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <cstdio> #include <string> int main() { std::string s = "10"; int i; if (sscanf(s.c_str(), "%d", &i) == 1) { std::cout << i << std::endl; } else { std::cout << "Bad Input"; } return 0; } |
6. Using range-based for-loop
Finally, the naive solution is to implement our own routine. The idea is to iterate through chars of the string using Range-based for-loop (C++11) and process each digit individually. The behavior is undefined if the converted value is out of range of integer data type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> int main() { std::string s = "10"; int i = 0; for (char c: s) { if (c >= '0' && c <= '9') { i = i * 10 + (c - '0'); } else { std::cout << "Bad Input"; return 1; } } std::cout << i << std::endl; return 0; } |
That’s all about converting a string to int in C++.