C++ – Convert int to string (3 ways)

In this article, we will learn about three different ways to convert an integer to a string in C++ i.e.

  1. Using to_string() function
  2. Using stingstream
  3. Using Boost’s lexical_cast() function.

Let’s discuss them one by one.

Convert int to string using to_string()

C++ provides a function std::to_string() for converting values of different types to string type. We can use this to convert an integer to string. For example,

#include <iostream>
#include <string>

int main()
{
    int num = 234;

    // Convert int to string 
    std::string num_str = std::to_string(num);

    std::cout<<num_str<<std::endl;

    return 0;
}

Output:

234

We passed an integer to the to_string() function, and it converted the given int to a string and returned the string object. If you pass a negative integer to the to_string(), it will convert to a string with a minus symbol.

Advertisements

Convert int to string using stringstream in C++

C++ provides a class stringstream, which provides a stream-like functionality. We can insert different types of elements in stringstream and get a final string object from the stream. Check out this example,

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

int main()
{
    int num = 234;

    std::stringstream stream;  
    
    // Add int to the stream
    stream<<num;  
    
    std::string str;  
    // Get string object from the stream
    stream>>str;  

    std::cout<<str<<std::endl;

    return 0;
}

Output:

234

In the stringstream, we inserted an integer, and the stream converted the integer to string. Then, in the end, we requested a string object from the stream.

Convert integer to string using boost::lexical_cast()

The C++ boost library provides a template function for data type conversion. It helps to convert elements from one data type to another. We can use that to convert an integer to a string. For example,

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

using namespace std;
using namespace boost;

int main()
{
    int num = 234;
    
    // Convert int to string using boost library
    string str = lexical_cast<string>(num);  
    
    cout<<str<<endl;

    return 0;
}

Output:

234

Summary:

We learned about three different ways to convert an integer to a string in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top