Many time during implementing c++ logic or displaying string we require to convert int to string in c++. We have given different solution for c++ int to string conversion. Here we have provided solutions using std::to_string(), using std::ostringstream and using custom function to convert int to string in c++.
1) Int to string c++ ( Using std::to_string)
#include<iostream>
#include <string>
using namespace std;
int main()
{
int nInput = 123;
string sOutput;
//Using C++ 11 int to string conversion
sOutput = to_string(nInput);
cout << "1) Using std::to_string():" << sOutput << endl;
return 0;
}
Here std:to_string function will convert into to string in c++. Generally this function is used in c++ 11. Here we will get input as string value “123”.
2) convert int to string c++ (Using std::ostringstream)
#include<iostream>
#include <sstream>
using namespace std;
std::string NumberToString(int Number)
{
std::ostringstream ss;
ss << Number;
return ss.str();
}
int main()
{
int nInput = 123;
string sOutput;
// c++ convert int to string using std::ostringstream
sOutput = NumberToString(nInput);
cout << "2) Using ostringstream:"<< sOutput << endl;
return 0;
}
Here we are using std::ostringstream for c++ int to string conversion. For using it we will require to add header : include <sstream> .
3) C++ Int to string using custom function
#include<iostream>
#include <algorithm>
using namespace std;
/**
* C++ version 0.4 std::string style "itoa":
* Contributions from Stuart Lowe, Ray-Yuan Sheu,
* Rodrigo de Salvo Braz, Luc Gallant, John Maloney
* and Brian Hunt
*/
std::string CustomIntToStr(int IntValue, int nBase)
{
std::string sBuf;
// check that the base if valid
if (nBase < 2 || nBase > 16) return sBuf;
enum { kMaxDigits = 35 };
sBuf.reserve(kMaxDigits); // Pre-allocate enough space.
int quotient = IntValue;
// Translating number to string with base:
do {
sBuf += "0123456789abcdef"[std::abs(quotient % nBase)];
quotient /= nBase;
} while (quotient);
// Append the negative sign
if (IntValue < 0) sBuf += '-';
std::reverse(sBuf.begin(), sBuf.end());
return sBuf;
}
int main()
{
int nInput = 123;
string sOutput;
// c++ int to string using cutom function
sOutput = CustomIntToStr(nInput, 10);
cout << "3) Using custom function: " << sOutput <<endl;
return 0;
}
We have used custom function which is similar to itoa(), It will be used for c++ int to string conversion.
If we are using boost library then also we can implement using including #include “boost/lexical_cast.hpp”, and then use lexical_cast<string>(Integer).
CONCLUSION:
In c++ program whenever we want to convert int to string in c++, we will require to use any of given methods. If our c++ compiler support c++11 or advance version then it will be more easy task because c++11 onward it is improving and you will find many functions in boost library as well. I hope given article will be useful for you to convert int to string c++ easily in c++ 11 or older versions as well.
Daya Shanker Prasad says
This is very easy, do the same thing during compile time. Convert int to string during compilation.