stoi in c++

#include <iostream>
#include <string>

int main() {
    std::string str = "123";
    int num = std::stoi(str);
    std::cout << "Converted integer: " << num << std::endl;
    return 0;
}
  1. Include Libraries:
  2. #include <iostream>: Includes the input/output stream library.
  3. #include <string>: Includes the string library.

  4. Declare Main Function:

  5. int main(): Declares the main function.

  6. Declare a String:

  7. std::string str = "123";: Declares a string variable named str and initializes it with the value "123".

  8. Convert String to Integer:

  9. int num = std::stoi(str);: Uses the std::stoi function to convert the string str to an integer and assigns the result to the variable num.

  10. Output Result:

  11. std::cout << "Converted integer: " << num << std::endl;: Prints the converted integer to the standard output.

  12. Return Statement:

  13. return 0;: Indicates that the program executed successfully and returns 0 to the operating system.