How to Use stoi in C++: Convert String to Integer Easily
In C++, use
stoi to convert a string containing digits into an integer. Call stoi with the string as an argument, and it returns the integer value. It throws exceptions if the string is not a valid number.Syntax
The stoi function converts a string to an int. Its basic syntax is:
int stoi(const std::string &str, std::size_t *pos = nullptr, int base = 10);
Explanation:
str: The string to convert.pos: Optional pointer to store the index of the first character after the number.base: Number base (default is 10 for decimal).
cpp
int stoi(const std::string &str, std::size_t *pos = nullptr, int base = 10);
Example
This example shows how to convert a string to an integer using stoi and handle invalid input with a try-catch block.
cpp
#include <iostream> #include <string> int main() { std::string numberStr = "12345"; try { int number = std::stoi(numberStr); std::cout << "The number is: " << number << std::endl; } catch (const std::invalid_argument& e) { std::cout << "Invalid input: not a number." << std::endl; } catch (const std::out_of_range& e) { std::cout << "Number out of range." << std::endl; } return 0; }
Output
The number is: 12345
Common Pitfalls
Common mistakes when using stoi include:
- Passing a string that contains non-digit characters causes
std::invalid_argumentexception. - Using a number too large for
intcausesstd::out_of_rangeexception. - Not handling exceptions can crash the program.
Always use try-catch blocks to handle these exceptions safely.
cpp
#include <iostream> #include <string> int main() { std::string badInput = "12abc"; try { int num = std::stoi(badInput); // Throws invalid_argument std::cout << num << std::endl; } catch (const std::invalid_argument& e) { std::cout << "Caught invalid_argument: input is not a pure number." << std::endl; } std::string largeNumber = "999999999999999999999"; try { int num = std::stoi(largeNumber); // Throws out_of_range std::cout << num << std::endl; } catch (const std::out_of_range& e) { std::cout << "Caught out_of_range: number too large for int." << std::endl; } return 0; }
Output
Caught invalid_argument: input is not a pure number.
Caught out_of_range: number too large for int.
Quick Reference
| Function | Description | Notes |
|---|---|---|
| stoi(string, pos, base) | Convert string to int | Throws exceptions on invalid input or overflow |
| stol(string, pos, base) | Convert string to long | Similar to stoi but returns long |
| stoll(string, pos, base) | Convert string to long long | For larger integers |
| stof(string, pos) | Convert string to float | For floating-point numbers |
| stod(string, pos) | Convert string to double | For double precision floats |
Key Takeaways
Use stoi to convert strings containing digits to int in C++ easily.
Always handle exceptions std::invalid_argument and std::out_of_range when using stoi.
The optional pos parameter can tell you where the number parsing stopped.
stoi defaults to base 10 but you can specify other bases like 16 for hex.
For larger numbers, consider stol or stoll instead of stoi.