Easiest and Best Way To Convert Int to String in C++

Do you want to convert Int to String in C++? This article will discuss all the possible and best ways you can use to convert Int to String in C++.

If you are using the C++ version higher than or equal to 11 then you can use the inbuilt functions such as stoi()[1] or to_string()[2] functions to convert the integer to String and vice versa in C++ easily. Let us see below how these works in the below example.

Method 1: Using to_string Function to Convert Int To String in C++

You can use the to_string() method present in the String class of C++. This method takes an integer value and returns it as a converted string value.

Let us see in the below example code the usage of to_string to convert int to string in C++.

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    int intToBeConverted = 50;

    //Using the to_string method to convert the above 
    //Integer to String
    string intString = to_string(intToBeConverted);

    //Printing the String 
    cout<<"The number converted to String is : "<<intString<<endl;

    return 0;
}

Output:

The number converted to String is : 50

As you can see in the above code, I successfully and easily converted the given integer of value 50 to the string stored in a string variable named intString.

Method 2: Using fmt::format_int Function To Convert Int to String

If you want a fast and alternative solution to convert the integer to string in C++ then you should use the fmt[3] library.

The format API is conceptually similar to the C printf family of functions, but it is safer, simpler, and several times faster than common standard library implementations.

Let us see in the below example code the usage of fmt library in C++.

#include <fmt/core.h>

int main() {

    //Converting the Integer 50 to string in safest way.
    std::string s = fmt::format("The Number Converted to String is {}.", 50);
    
}

Output:

The Number Converted to String is 50.

This works similarly to Python. But this conversion is the safest conversion and this library takes care of all the exceptions that can occur while conversion and also executes faster than the to_string function.

Method 3: Using ostringstream From the sstream libary to Convert Int to String

Another method that you can use is using the ostringstream[4] function from the sstream library. This method follows two steps for conversion first is outputting the value of the number to the stream and then in the second step, it get the string with the contents of the stream.

Let us see in the below example code the usage of ostringstream from sstream library for C++.

#include <iostream>
#include <sstream>

using namespace std;
 
int main()
{
    int intToBeConverted = 342;

    //Using the ostringstream to convert the number
    ostringstream intConvert;

    intConvert << intToBeConverted;

    //Initializing the String to get the String Version of Stream
    string getString = intConvert.str();

    //Printing the String 
    cout<<"The number converted to String is : "<<getString<<endl;

    return 0;
}

Output:

The number converted to String is : 342

As you can see from the above code that using the stream method I was able to convert the int to string. And hence if you are having or getting your numbers or integers in a stream then this method should be used to get the conversion to be done faster.

Easiest and Best Way To Convert Int to String in C++

Wrap Up

I hope you were able to get the answer related to how to easily convert the int to string in C++. I have listed the best 3 Methods that you can use as per your requirement that suit you the best.

Let me know if you have any better method than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Convert a Char to String in C++ Quickly
  2. Parse (split) or Tokenize a String in C++ Using Delimiter
  3. Different Ways to Sort a Vector In C++
  4. std::stoi Convert String To Numbers in C++ [3 Ways]

Leave a Comment