如何清除流字符串的缓冲区? [英] How to clear the buffer of a streamstring?

查看:164
本文介绍了如何清除流字符串的缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个循环的流字符串,它正在烧我的RAM。那么如何正确清除蒸汽串的缓冲区呢?就像这样简化:

I have a streamstring in two loops and it is burning my RAM. So how to clear properly the buffer of a steamstring? It is like that to simplify :

stringstream ss (stringstream::in | stringstream::out);

for()
{
    for()
    {
        val = 2;
        ss << 2;
        mystring = ss.str();
        // my stuff
    }
    // Clear the buffer here
}

它写了2然后22然后是222 ...我尝试了.clear()或.flush(),但事实并非如此。那么我该怎么做呢?

It wrote 2 then 22 then 222... I tried .clear() or .flush() but it is not that. So how I do this?

推荐答案

显而易见的解决方案是使用新的 stringstream ,例如:

The obvious solution is to use a new stringstream each time, e.g.:

for (...) {
    std::stringstream ss;
    for (...) {
        //  ...
    }
}

这是设计使用 stringstream 的方式。 (也:您
真的想要 stringstream 还是只是 ostringstream ?)

This is the way stringstream was designed to be used. (Also: do you really want a stringstream, or just an ostringstream?)

这篇关于如何清除流字符串的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆