views:

56

answers:

3

Hi All,

I'm using VS2008 C++.

As I understand it there is no way to pass something like this in a C++ stream : (without using external libraries)

"number " << i    <------ when i is an integer.

So I was looking for a better way to do this, and I all I could come up with is create a string using :

char fullstring = new char[10];
sprintf(fullString, "number %d", i);
.... pass fullstring to the stream  .....
delete[] fullString;

I know it's stupid, but is there a better way of doing this?

+3  A: 
std::ostringstream oss;
oss << "number " << i;
call_some_func_with_string(oss.str());
sbi
+4  A: 

Did you even bother to try?

int i = 3;
std::cout << "number " << i;

Works quite fine, and naturally the same should work with any stream.

Matti Virkkunen
+2  A: 

try this:

#include <sstream>
// [...]
std::ostringstream buffer;
int i = 5;
buffer << "number " << i;
std::string thestring = buffer.str(); // this is the droid you are looking for
utnapistim