String to int c++ : Searching for how can I convert string to int c++, you are in right place and we are here, in this article we cover the top 6 ways to do it.
The following are the 6 functions that can be leveraged to achieve string to integer conversion.
Contents
Convert String To Int C++
Convert string to int C++ is very simple, Here we discussed the 6 ways to convert string to int c++, Just click the links above on tables of content to reach direct to the heading or check all the way to explore the best possible methods.
- string streams,
- std::atoi
- std::stoi
- boost::lexical_cast
- sscanf
- for loop with Range
string streams
The first and also I love this way to convert string to int C++ and I know you also love.
To convert from string representation to integer value, we can use std::stringstream.
if the value converted is out of range for the integer data type, it returns INT_MIN or INT_MAX.
Also if the string value can’t be represented as a valid int data type, then 0 is returned.
#include
#include
#include
int main() {
std::string x = "50";
int y;
std::istringstream(x) >> y;
std::cout << y << '\n';
return 0;
}
Output:
50
As per the above output, we can see it converted from string numbers to an integer numbers.
std::atoi
This comes in our 2nd list to learn how to convert string to int c++,
Lets use std::atoi function to convert a string variable to an int value.
This function takes c string as argument or parameter.
For this, we need to parse the input string to a c string, which can be done using std::string::c_str.
Here std::string::c_str will help as the parse method.
Finally, the converted value will be passed to std:atoi function.
#include
#include
#include
int main() {
std::string x = "50";
int y = std::atoi(x.c_str());
std::cout << y << '\n';
return 0;
}
Output:
50
Also, Check,
Convert List To String In Java
std::stoi
3rd and most powerful way to convert string to int c++,
For converting a string to an integer numeric type, the traditional approach is to use std::stoi function.
stoi was part of the C++11 version.
Passing valid integers will provide the right input.
In case of invalid or bad input, this function throws integer overflow or std::out_of_range or std::invalid_argument exception.
#include
#include
int main() {
std::string x = "50";
try
{
int y = std::stoi(x);
std::cout << y << '\n';
}
catch (std::invalid_argument const &ex)
{
std::cout << "Invalid input" << '\n';
}
catch (std::out_of_range const &ex)
{
std::cout << "Integer overflow" << '\n';
}
return 0;
}
Output:
50
boost::lexical_cast
boost::lexical_cast is our 4th way to convert string to int c++.
We can also use boost::lexical_cast to convert string to an integer.
In case of any issue, it throws boost::bad_lexical_cast exception.
#include <boost/lexical_cast.hpp>
#include
#include
int main() {
std::string x = "50";
int y;
try {
y = boost::lexical_cast(x);
std::cout << y << '\n';
}
catch (boost::bad_lexical_cast const &ex)
{
std::cout << "error" << '\n'; } return 0; }
b>Output:
50
sscanf
My 5th list “sscanf” is lovely to convert it.
We can leverage scanf function to convert string to an integer type.
This function accepts c string as a parameter.
#include
#include
#include
int main() {
std::string x = "50";
int y;
if (sscanf(x.c_str(), "%d", &y) == 1) {
std::cout << y << '\n';
} else {
std::cout << "Invalid Input";
}
return 0;
}
Output:
50
You may like,
for loop with Range
This is our last way to convert string to int C++.
We can write custom for loop function to convert string to an integer numeric value.
The aim is to loop the characters of string using range-based for loop.
This function accepts c string as a parameter.
#include
#include
int main()
{
std::string x = "50";
int y = 0;
for (char c: x)
{
if (c >= '0' && c <= '9') {
y = y * 10 + (c - '0');
}
else {
std::cout << "Invalid Input";
return 1;
}
}
std::cout << y << '\n';
return 0;
}
Output:
50
Also, we can use printf to print the format specifier like float, integer, character, string, octal, and hexadecimal.
Note: if you pass floating-point values i.e passing decimal value as a decimal string, then the final result will still be an integer(decimal rounded off to integer).
To conclude this tutorial, we covered various types of implementation to achieve string to int c++ conversion.