我已经尝试了好几种方法,

std::stringstream m;
m.empty();
m.clear();

这两种方法都行不通。


当前回答

在gnu c++中,这些函数不会丢弃stringstream中的数据

    m.str("");
    m.str() = "";
    m.str(std::string());

以下为我清空stringstream:

    m.str().clear();

其他回答

我的观点是:

this seemed to work for me in xcode and dev-c++, I had a program in the form of a menu that if executed iteratively as per the request of a user will fill up a stringstream variable which would work ok the first time the code would run but would not clear the stringstream the next time the user will run the same code. but the two lines of code below finally cleared up the stringstream variable everytime before filling up the string variable. (2 hours of trial and error and google searches), btw, using each line on their own would not do the trick.

//clear the stringstream variable

sstm.str("");
sstm.clear();

//fill up the streamstream variable
sstm << "crap" << "morecrap";

在gnu c++中,这些函数不会丢弃stringstream中的数据

    m.str("");
    m.str() = "";
    m.str(std::string());

以下为我清空stringstream:

    m.str().clear();

这是一个概念问题。

Stringstream是一个流,所以它的迭代器是向前的,不能返回。在输出stringstream中,需要flush()来重新初始化它,就像在任何其他输出流中一样。

我一直在定义它的范围:

{
    std::stringstream ss;
    ss << "what";
}

{
    std::stringstream ss;
    ss << "the";
}

{
    std::stringstream ss;
    ss << "heck";
}

这应该是最可靠的方式,不管编译器:

m=std::stringstream();