C++

Stoi C++

This article will go through the notion of the stoi() function in the C++ programming language. Stoi is an abbreviation of “string to an integer”. The stoi() function in plain language simply transforms strings into integers in the C++ programming language. As we all know, the C++ programming language is one of the privileged programming languages that allows data type conversion by using different functions and methods, and the stoi function is a great example of this claim.

Converting data types in C++:

In C++ programming, converting data types is fairly common. Type casting is another term for it. Data type conversion is often known as type casting. There are two types of casting or type conversion in the C++ programming language: Implicit and explicit type casting.

So, if you want to convert a string data type variable into an integer in C++ programming, we have several methods defined in the C++ header file libraries. These methods are listed below:

  1. The atoi () function
  2. The stringstream () class
  3. The stoi () function
  4. The atoi() Function:

The atoi () function converts the null-terminated string containing the null character “\0” into an integer data type variable. The atoi () function takes the string variable as a parameter, converts it into an integer variable and stores it into a new variable in which it is called. The syntax for the atoi function in the C++ programming language is written below.

# int i = atoi(str) ;

The stringstream () Class:

In C++, a stringstream class is a Stream Class. The stringstream class implements memory-based stream input/output operations, that is, string. A string object can be handled as a stream in C++ using the stringstream class. It is used to manipulate strings. We can do extraction and insertion operations from/to strings by considering them as streams, exactly as cin and cout streams. These operations are commonly used to transform string data types into numeric data types and vice versa. The stringstream class is also useful for a variety of parsing tasks. Now we will look into the syntax for using this method in C++ programming:

# stringstream(str) >> n

“Str” is the string variable that we want to be converted, and “n” is the integer that will store the converted value of the string variable.

Now we will discuss the stoi() function in C++ programming.

The stoi() function:

This function in C++ is used to convert the numeric strings to integers. The capability of stoi can also be used to delete other material from the string, such as trailing characters. So, C++ programmers use the stoi () function to change the string data type variables into an integer data type. The syntax for the stoi () function in the C++ programming language is written below:

# int n = stoi(str) ;

The stoi() function in Ubuntu 20.04:

Let us implement the stoi () function in the Ubuntu 20.04 environment to better understand this function. For that, we must first learn that in the Ubuntu environment, C++ program files are saved with the extension “.cpp.” To do so, open a terminal and type “cd Desktop” on the command line, then press enter and type “touch filename.cpp” to create a file with the “.cpp” extension on our desktop.

Converting the string variable into an integer variable using the stoi() function in C++:

The below code attached in the appended image explains that we initially had two header libraries. The first one is an iostream and the second and most important one is a string. After that, we have to start writing our main function. We have stated a variable titled “str” with a data type of string and assigned it a value. After that, we have declared a variable with an integer data type. We have passed a string to the stoi() function. Then we used a cout statement to get a print of the “Number” variable and ended the main function.

Graphical user interface, text, application

Description automatically generated

Close the file after pressing the save button. To create the output file, return to the terminal and type “g++” with your file name and the “.cpp” extension. A file with the extension of “.out” will be created by this command. By entering the “./” command followed by your “.out” extension, you can now run this file to get the desired output.

Text

Description automatically generated

We have successfully transformed the string variable “str” into an integer variable named “Number” using the stoi() function in C++ programming, as the “Number” integer is displayed rather than the string variable “str.” We will look into a string variable with other characters and even with alphabets to see how the stoi() function reacts.

Converting the string variable into an integer with a character using stoi() function in C++:

First, we will convert a string with a character into an integer and see how it will be converted. To implement this in Ubuntu, we will follow the above-mentioned steps to write a piece of code in a “.cpp” file.

Text, letter

Description automatically generated

In the code sample above, we have iostream and string libraries. Then we came towards the main() function having integer datatype. We have declared a variable “str” with a string data type and assigned it a value of “-4”. We used a minus sign to start a string variable and then converted it to an integer using the stoi() function. The answer will be stored in a variable titled “I” with the integer data type and finally printed. Let us look at the output snippet of this code below:

Text

Description automatically generated with low confidence

As you can see in the above output, the stoi () function converts the minus sign as well present in the string.

Converting the string variable into an integer with characters before and after the numbers using stoi() function in C++:

We have converted the string value in an integer using a single character, but what if there are other characters in the string before or after the numbers? Let’s see in the example below:

Text

Description automatically generated

In the above code, we have again utilized iostream and string libraries. Then we came towards the main() function having integer datatype. We have declared a variable “str” with a string data type and assigned it a value of “1+2”. You can check that we have used the increment operator in the code. Then we converted the value to an integer by using the stoi() function as depicted in the above code. The answer will be stored in a variable titled “i” with the integer data type and finally printed. Let us look at the output snippet of this code below:

We added a plus sign between two numbers in the string and converted it into an integer using the stoi () function.

A screenshot of a computer Description automatically generated with medium confidence

As the output shows that the stoi () function only stored the first number and disregarded the character and the other number after it.

Converting the string variable into an integer with alphabets and numbers using stoi() function in C++:

In this example, we will be adding alphabets and numbers in the string value to convert it into integer values using the stoi () function in C++. We will execute the following code to ensure will the string variable converts integer variables, including alphabets and numbers, using the stoi () function:

Text Description automatically generated

The above output explains that the stoi () function completely ignored the alphabets in the string and only stored the number when converting the string. So, we have concluded that only the first number will be stored in the string while using the stoi() function for conversion.

A screenshot of a computer Description automatically generated with medium confidence

Conclusion:

This article discussed the stoi () function in the C++ Programming language. The use of methods and functions for data type conversion was also explained. We also went through the different types of data type conversion that are allowed in C++ programming. In the end, the stoi function was also implemented in the C++ programming language in the Ubuntu 20.04 environment with different types of additions in the string variable to help us get a better grip on its working.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.