String to int Conversion Using stoi() Function

The stoi() function in C++ takes a string as an argument and returns its value in integer form. This approach is popular in current versions of C++, as it was first introduced in C++11.

 If you observe stoi() a little closer you will find out that it stands for:

Breakdown of stoi() in simple terms

Syntax:

int stoi(string str, size_t position = 0, int base = 10);

Parameters:

  • str: string to be converted. (compulsory)
  • position: starting position. (optional with default value = 0)
  • base: base of the number system. (optional with default value = 10)

Example:

C++




// C++ program to demonstrate
// working of stoi()
// Work only if compiler supports
// C++11 or above. Because STOI()
// was added in C++ after 2011
#include <iostream>
#include <string>
using namespace std;
 
// Driver code
int main()
{
  string str1 = "45";
  string str2 = "3.14159";
  char str3[] = "31337 geek";
 
  // type of explicit type casting
  int myint1 = stoi(str1);
   
  // type of explicit type casting
  int myint2 = stoi(str2);
     
  // type of explicit type casting
  int myint3 = stoi(str3);
 
  cout << "stoi(\"" << str1 <<
          "\") is " << myint1 << '\n';
    cout << "stoi(\"" << str2 <<
            "\") is " << myint2 << '\n';
    cout << "stoi(\"" << str3 <<
            "\") is " << myint3 << '\n';
    return 0;
}


Output

stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337

As we can see, stoi() method supports both C++ style and C style strings.

We can also use atoi() function for performing this task.

Convert String to int in C++

Converting a string to int is one of the most frequently encountered tasks in C++. As both string and int are not in the same object hierarchy, we cannot perform implicit or explicit type casting as we can do in case of double to int or float to int conversion. Conversion is mostly done so that we can convert numbers that are stored as strings.

Example:

str=”191″

num=191

There are 5 significant methods to convert strings to numbers in C++ as follows:

  1. Using stoi() function
  2. Using atoi() function
  3. Using stringstream
  4. Using sscanf() function
  5. Using for Loop
  6. Using strtol() function

Similar Reads

1. String to int Conversion Using stoi() Function

The stoi() function in C++ takes a string as an argument and returns its value in integer form. This approach is popular in current versions of C++, as it was first introduced in C++11....

2. String to int Conversion Using atoi()

...

3. String to int Conversion Using stringstream Class

The atoi() function in C++ takes a character array or string literal as an argument and returns its value in an integer. It is defined in the header file. This function is inherited by C++ from C language so it only works on C style strings i.e. array of characters....

4. Converting String to int Using sscanf()

...

5. Using For Loop Convert Strings into int

The stringstream class in C++  allows us to associate a string to be read as if it were a stream. We can use it to easily convert strings of digits into ints, floats, or doubles. The stringstream class is defined inside the header file....

6. String to int Conversion Using strtol()

...

Contact Us