I was solving a problem in TopCoder. It required a conversion of integer to a string. This is simple in Java but a little complicated in C++. So, I am jotting down here some of the link that helped me. I will also try to make a mention of the problem associated with them.
The first one – itoa. Well, itoa considered unsafe to use this in C++. You may get an error like ” itoa was not declared in the scope “. I got stuck with the same error. It was then I started searching for solutions. The link following is about itoa. Note that, itoa doesn’t work in TopCoder.
The next link use boost and a string stream. Again, the header file “boost/lexical_cast.hpp” is not detected by TopCoder.
So, I feel, more or less the best way is to have you own customized code.
string convertInt(int number)
if (number == 0)
for (int i=0;i<temp.length();i++)
The following link has more information about it.
If you have any doubt, post in them as comments.
In Java, you can go about with some code like the following. Note, ‘s’ is a String and ‘a’,’ b’ and ‘c’ are Integers.
s = a + ” ” + b + ” ” + c;
Though something like above is possible in C++, it requires ‘a’,’ b’ and ‘c’ to be Strings.
Happy coding ! 🙂