C++ Program - Convert String to Integer

C++ Convert String to Integer

C++ String to Integer

You can convert a string to integer in C++ in many ways.

To convert a string to integer, we will use stoi() function, atoi() function or stringstream.

In this tutorial, we will look into the above said processes, with syntax and example programs.

Convert String to Integer using stoi()

To convert a string to integer in C++, you can use stoi() function. The function template of stoi() is given below.

int stoi (const wstring& str, size_t* idx = 0, int base = 10);

stoi() parses str (first argument) as a number and returns an integer.

You can also pass the base (octa, deci, hex, etc.,) using which you would like parse the string to integer.

In the following example, we shall use stoi() function to convert a string to integer.

C++ Program

#include using namespace std; int main() {

string str1 = "512"; int n = stoi(str1); cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download