How to convert std string to int in C++

In this article, we will discuss various methods to convert std::string to int in C++.

1.Convert string to int using atoi function

The atoi function parses C-style string (null-terminated) content as an integer.

Syntax of atoi

Defined in header <cstdlib> (stdlib.h)

int atoi (const char * str);

Arguments

  • str – C- style null-terminated string

Returns

  • On success, returns the converted number as integer value.

If the converted number’s value is more than the int can hold it can cause undefined behavior. For this scenario we can atol function it will convert string to a long integer.

Example

We will convert std string to C string using c_str(). It will return a pointer(const char*) to an array that contains a null-terminated sequence of characters.

#include <iostream>

using namespace std;

int main()
{
   string str_num = "42";
   
   // We will convert std string to C string using c_str()
   int num = atoi(str_num.c_str());
   
   cout << num;
   
   return 0;
}

Output

42

2.Convert string to int using stoi function

We can use stoi function to convert string to int. The C++ 11 introduced stoi function in std::string so the older C++ versions will not support this function.

This function parses the content of str as an integer number of the specified base. This function has two variants, one for the string and the other one for the wide string (wstring).

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

Parameters

  • str – String contains the integer representation
  • idx – Pointer to an object of type size_t , this value can be null pointer
  • base – Numerical base, default value is base 10

Returns

  • On success, stoi returns the converted integer number as an int value.

Exceptions

  • std::invalid_argument – No conversion performed
  • std::out_of_range – If the converted value fall out of the range of the result
Example
#include <iostream>

using namespace std;

int main()
{
  string str_num = "42";

  int num = stoi(str_num);

  cout << num;
   
  return 0;
}

Output

42

3. Convert string to int using stringstream

We can use stringstream to convert string to int.

#include <iostream>
#include <sstream> //For string stream

using namespace std;

int main() 
{
    string str_num = "42";

    stringstream num_ss(str_num); //Initialize stringstream by passing string
    int num = 0;
    num_ss >> num;

    cout << "Number :  " <<num;
    
    return 0;
}

Output

Number : 42 

4. Convert string to int using sscanf

We can convert string to int using sscanf function.

Syntax of sscanf function
int sscanf ( const char * s, const char * format, ...);

Parameters

  • s – C- string to process
  • format – the required format
  • … (additional arguments) – Depends on the format a number arguments

Returns

  • On success, the function returns the number of items in the argument list successfully filled
#include <iostream>

using namespace std;
 
int main() 
{
    string str_num = "42";
    int num = 0;

    // Convert std::string to C-string using c_str() and pass it to sscanf
    if (sscanf(str_num.c_str(),"%d",&num) == 1)
        cout << "Number :  " <<num;
    else
        cout <<"Bad input";
    
    return 0;
}

Output

Number : 42 

5. Using boost::lexical_cast

Boost library provides a cast operator, boost::lexical_cast it can convert string to numeric types int or double and vice versa.

#include <iostream>
#include <boost/lexical_cast.hpp>

using namespace std;

int main()
{
    string str_num = "42";
    int num=0;
 
    try {
        num = boost::lexical_cast<int>(str_num);
        cout << num ;
    }
    catch (boost::bad_lexical_cast const &e) {        
        cerr << e.what() <<endl;
    }
 
    return 0;
}

Output

42

Leave a Comment