Stoi function in C++
C++ provides a variety of string manipulation functions that allow you to parse, convert, and manipulate strings. One such function is stoi(), which is a part of the <string> header in C++. The function stoi stands for "string to integer", and it converts a string to an integer. In this blog, we will discuss the stoi function in detail, including its syntax, code examples, inputs, and outputs.
Syntax of the stoi() function :
The syntax of the stoi function is quite straightforward. Here is an example of how to use it:
int stoi (const string& str, size_t *idx = 0, int base = 10);
long stoi (const string& str, size_t *idx = 0, int base = 10);
long longstoi (const string& str, size_t *idx = 0, int base = 10);
The stoi function takes three parameters:
- str: It is the string that we want to convert to an integer.
- idx: It is an optional parameter that can be used to store the number of characters processed in the string.
- base: It is an optional parameter that can be used to specify the number base of the integer to be returned. By default, it is set to 10, which means that the integer returned is in base 10.
Code Examples of stoi() function Let's look at some code examples that demonstrate how to use the stoi function in C++.
Example 1:Simple Conversion
In this example, we will convert a simple string to an integer.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = stoi(str);
cout<< num <<endl;
return 0;
}
Output:
12345
Example 2:Conversion with idx parameter:
In this example, we will use the idx parameter to store the number of characters processed in the string.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345abc";
size_tidx;
int num = stoi(str, &idx);
cout<< num <<endl;
cout<< "Number of characters processed: " <<idx<<endl;
return 0;
}
Output:
12345
Number of characters processed: 5
Example 3:Conversion with base parameter:
In this example, we will use the base parameter to specify the number base of the integer to be returned.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "1010";
int num = stoi(str, 0, 2);
cout<< num <<endl;
return 0;
}
Output:
10
Input and Output of the stoi() function. The stoi function takes a string as input and returns an integer as output. The input string can be a combination of numerical characters and other characters. The function only processes the numerical characters and converts them to an integer. The other characters are ignored.
For example, consider the following string: "12345abc". The stoi function will only process the characters "12345" and convert them to an integer. The characters "abc" will be ignored.
The output of the stoi function is an integer that represents the numerical value of the input string. If the input string is not a valid numerical value, the function will throw an exception of type invalid_argument.
Exceptions: The stoi function can throw several exceptions, including:
- invalid_argument: This exception is thrown when the input string is not a valid numerical value.
- out_of_range: This exception is thrown when the converted integer is too large to be stored in an int data type.
It is recommended to use try-catch blocks to handle these exceptions. Here is an example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abc";
try {
int num = stoi(str);
cout<< num <<endl;
} catch (const invalid_argument& e) {
cout<< "Invalid argument: " <<e.what() <<endl;
} catch (const out_of_range& e) {
cout<< "Out of range: " <<e.what() <<endl;
}
return 0;
}
Output:
Invalid argument: stoi: no conversion
Conclusion:
The stoi function in C++ is a convenient and efficient way to convert a string to an integer. It is an important tool for string manipulation and parsing, and is widely used in C++ programming. Understanding the syntax, code examples, inputs, and outputs of the stoi function will help you use it effectively in your own programs.