Converting integers to strings always seems confusing to most the programmers at beginning. In this article, we are going to learn about how to convert string to int using various methods in C++.
What is int & string in C++?
There are many data types in C++, but two important ones are int & string. Let's first revise them a bit before moving on.
The int is a built-in data type that is used to represent integers or whole numbers. t can represent both positive and negative numbers. On the other hand, the string is a built-in data type that is used to represent a sequence of characters. It is defined in the header file. A string is basically an object that can store a sequence of characters, including letters, numbers, and punctuation.
We may want to convert a string to an int for various reasons. One important one is Input validation, where we take input for numerical value as a string and then convert it to an int to ensure that it is a valid numerical value. Also, sometimes when we have to process large amounts of data, we may need to read numbers from files, which are often represented as strings.
How to convert string to int in C++?
There are multiple methods to achieve the conversion from string to int. We will discuss 3 common methods available in C++ programming:
- Using stoi() function
- Using atoi() function
- Using Stringstream
1) Using stoi() function
The best way to convert string to int in C++ is by using stoi function. The stoi, which stands for the string to integer, is part of the 'string' header. It takes a string as an argument and returns its value in integer form.
Learn more about stoi in C++ here.
Consider the below example to completely understand the working of the stoi() function :
Let's consider another example:
2) Using atoi() function
In C++, the atoi is a built-in function that converts a C-style string to an integer. It stands for "ASCII to integer", and it is defined in the header file. Here we perform explicit type casting.
Consider the C++ code below to completely understand the working of the atoi() function:
Note that atoi() is a legacy C-style function, while stoi() is added in C++ 11. Basically, atoi() takes only one parameter and returns an integer value. The stoi() can take up to three parameters, the second parameter is for starting index and the third parameter is for the base of the input number.
3) Using stringstream
The stringstream is a class defined in the 'sstream' header that allows you to treat strings as input/output streams. It is useful for many tasks, such as parsing a string to extract numerical values or formatting numerical values as a string. This is a method to convert string to int in C++ without stoi.
Since to access the data members and member functions of a class, we create an object first. Similarly, we will create an object of Stringstream class and we use different operators for conversions. We then take input or read a string from the string variable, then this stringstream object does some internal conversions from string to int and then we read an int value using int variable.
Here is a C++ example below:
Also, check out how to convert char to string in C++.
We hope you now have a complete understanding of the multiple methods to convert a string to int in C++. Congratulations on getting this far! Now give yourself a pat on the back. Good job!