开发者

C++ convert formatted string to a stream

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 cr开发者_StackOverfloweate 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?


std::ostringstream oss;
oss << "number " << i;
call_some_func_with_string(oss.str());


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜