c++ - 我怎样才能释放 ostringstream?

标签 c++

我有以下代码:

int n=2;
ostringstream convert;   // stream used for the conversion
convert << n; 
string query= convert.str();

如何释放 ostringstream?

最佳答案

使用生命周期管理:

std::string query;
int n = 2;

{
    std::ostringstream oss;
    oss << n;
    query = oss.str();
}

更短,但更难阅读:

int n = 2;
std::string query
          = static_cast<std::ostringstream &>(std::ostringstream() << n).str();

可能更好,具体取决于您的情况:

auto query = std::to_string(2);

关于c++ - 我怎样才能释放 ostringstream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851214/

相关文章:

c++ - 文件关闭通知

c++ - 定义模板化静态类成员的不同实例化

c++ - valgrind 改变二进制行为

c++ - 使用模板的嵌套类中的未知类型名称 'class'

c++ - 如何在 C++ 中遍历 void* 的字节?

C++ 宏之谜 : Printing the name of the TYPE

c++ - boost::lockfree 中的 "wait-free"到底是什么意思?

c++ - 捕获鼠标在 Win32 游戏中的文本框上的点击

c++ - 使用 Cg :compile error:semantic "POSITION" not visible in this profile 进行体绘制

c++ - 使用标准编辑器打开文件并跳转到特定行