C++ 将格式化字符串转换为流

标签 c++ stream printf

我正在使用 VS2008 C++。

据我所知,没有办法在 C++ 流中传递这样的东西:(不使用外部库)

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

所以我一直在寻找一种更好的方法来做到这一点,我所能想到的就是使用以下方法创建一个字符串:

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

我知道这很愚蠢,但是有更好的方法吗?

最佳答案

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

关于C++ 将格式化字符串转换为流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2955977/

相关文章:

java - Java 中的简单 Web 代理

下面这段代码的 C++ 等效项 (sprintf)

c - 警告 : format ‘%s’ expects argument of type ‘char *’ , 但参数 2 的类型为 ‘int’ 使用 argv

C++ 使用运算符重载转换模板

c++ - "Was not declared in this scope"结构定义错误。 C++

c++ - 使用英特尔 MPI 构建 Boost.MPI

java - 什么时候应该开始使用字符串替换而不是 sprintf?

c++ - 如何将一个数组重新分配给另一个数组?

c# - 在功能列表操作中,我们怎么称呼 "inserting something between each item"?

c++ - 有效使用C++ iomanip库