C++ Converting a String to an Integer

In C++, there are several ways to convert a string to an integer. This can be useful in various scenarios, such as when you need to perform mathematical operations on a user input or when you want to manipulate numerical data stored as strings. In this article, we will explore some common methods for converting a string to an integer in C++, along with examples.

Method 1: Using stoi()

One of the simplest ways to convert a string to an integer in C++ is by using the stoi() function. This function is part of the std namespace and is available in the <string> header. It takes a string as input and returns the corresponding integer value.

Here is an example:

#include <iostream>
#include <string>

int main() {
    std::string str = "123";
    int num = std::stoi(str);
    
    std::cout << "The converted integer is: " << num << std::endl;
    
    return 0;
}

Output:

The converted integer is: 123

As you can see, the stoi() function successfully converts the string “123” to the integer value 123.

Method 2: Using stringstream

Another approach to convert a string to an integer in C++ is by using a stringstream. This method involves creating a stringstream object, feeding the string into it, and extracting the integer value using the extraction operator (>>).

Here is an example:

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string str = "456";
    std::stringstream ss(str);
    int num;
    
    ss >> num;
    
    std::cout << "The converted integer is: " << num << std::endl;
    
    return 0;
}

Output:

The converted integer is: 456

In this example, we create a stringstream object ss and pass the string “456” to it. Then, we extract the integer value using the extraction operator (>>) and store it in the variable num.

Method 3: Using strtol()

The strtol() function is another option for converting a string to an integer in C++. It is a C library function that can also be used in C++ programs. This function takes a string, a pointer to a character pointer, and a base as input, and returns the converted integer value.

Here is an example:

#include <iostream>
#include <cstdlib>
#include <string>

int main() {
    std::string str = "789";
    char* end;
    int num = std::strtol(str.c_str(), &end, 10);
    
    std::cout << "The converted integer is: " << num << std::endl;
    
    return 0;
}

Output:

The converted integer is: 789

In this example, we use the strtol() function to convert the string “789” to the integer value 789. The c_str() function is used to obtain a C-style string from the std::string object, and the &end parameter is used to capture the end of the converted string.

Method 4: Using Boost.Lexical_cast

If you have the Boost library installed, you can also use the lexical_cast function to convert a string to an integer. This function provides a convenient way to perform type conversions in C++.

Here is an example:

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

int main() {
    std::string str = "987";
    int num = boost::lexical_cast<int>(str);
    
    std::cout << "The converted integer is: " << num << std::endl;
    
    return 0;
}

Output:

The converted integer is: 987

In this example, we use the lexical_cast function from the boost namespace to convert the string “987” to the integer value 987.

These are just a few methods for converting a string to an integer in C++. Depending on your specific requirements and the libraries available to you, there may be other options as well. It is important to choose the method that best suits your needs and ensures accurate conversion.

Remember to handle any potential errors or exceptions that may occur during the conversion process, such as invalid input or overflow conditions. This will help ensure the reliability and robustness of your code.

Scroll to Top