converting Int to String

So I've got an issue with adding an int/float into a string. I know that adding, say an int equal to 12 into the middle of a string will place a female sign instead of the actual numbers 12, so should I create a function that resets a number variable to the correct number in the index, or is there a function already available that would do this?
I'm just worried about slowing down the computer since I'm trying to do this inside of multiple graphics to change their images, I figure that a function provided by C++ std would just work faster than anything that I come up with.
Also I don't really know how I would work this out with a decimal point, though I don't plan on using one it may come in handy later on.

example of how I'm saving images for an animation;
image1.png
image2.png
image3.png
image4.png

(so I just have to change the number with a while loop and I get an easy animation)
Last edited on
C++11 provides to_string() functions. Your compiler may or may not implement them.
http://en.cppreference.com/w/cpp/string/basic_string/to_string

Another approach would be to use std::stringstream, I guess?
http://www.cplusplus.com/reference/iostream/stringstream/

Edit: you != your.
Last edited on
I'm using visual c++ 2010, so I guess I can't use the first link, though that is definitely what I want. I'll have to look at the second link when I'm a bit more awake, maybe it'll make more sense then.
Thank you for the help.
I don't want to spoil your learning fun, but then again this is a kludge which will be replaced by proper code when you upgrade to Visual Studio 11. (I'm using GCC, but it should work for you too.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>

template <typename T>
std::string number_to_string(T number)
{
	return dynamic_cast<std::stringstream *> (&(std::stringstream() << number))->str();
}

int main()
{
	std::string s = number_to_string(143.4) + '\t' + number_to_string(-43);

	std::cout << s << std::endl;
}

Topic archived. No new replies allowed.