C++ string to int Conversion
We can convert string to int in multiple ways. The easiest way to do this is by using the std::stoi() function introduced in C++11.
Example 1: C++ string to int Using stoi()
Output
123
Example 2: char Array to int Using atoi()
We can convert a char array to int using the std::atoi() function. The atoi() function is defined in the cstdlib header file.
Output
num = 456
To learn other ways of converting strings to integers, visit Different Ways to Convert C++ string to int
C++ int to string Conversion
We can convert int to string using the C++11 std::to_string() function. For older versions of C++, we can use std::stringstream objects.
Example 3: C++ int to string Using to_string()
Output
123
Example 4: C++ int to string Using stringstream
Output
15
To know about converting a string to float/double, visit C++ String to float/double.