What is the method to convert a string to an integer in C++?

In C++, you can use the std::stoi() function to convert a string to an integer. Here is how you can use it:

#include <iostream>
#include <string>

int main() {
    std::string str = "12345";
    int num = std::stoi(str);
    std::cout << num << std::endl;

    return 0;
}

This will result in:

12345

The std::stoi() function also provides some additional parameters, such as specifying the base (binary, octal, decimal, hexadecimal, etc.), handling leading whitespace characters, and checking the success of the conversion. Refer to the C++ documentation for more detailed information on the std::stoi() function.

bannerAds