In C++, you can convert a string to an integer using the stoi function. Here is an example:
``c++
#include
#include
int main() {
std::string str = "123";
int myInt = std::stoi(str);
std::cout << "The string is: " << str << std::endl;
std::cout << "The integer is: " << myInt << std::endl;
return 0;
}
str
In this example, we first declare a string variable namedand initialize it with the value of "123". We then declare an integer variable namedmyIntand use thestoifunction to convert thestrvariable to an integer.strFinally, we print out
andmyIntusingcoutstatements. The output of this program will be:
The string is: 123
The integer is: 123
`
Note that if the string cannot be converted to an integer, the
stoi function will throw a std::invalid_argument or std::out_of_range` exception. You should be aware of this and handle these exceptions appropriately in your code.