C++ 如何清除 stringstream 变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:16:06  来源:igfitidea点击:

How do you clear a stringstream variable?

c++stringstream

提问by CodingWithoutComments

I've tried several things already,

我已经尝试了几件事,

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

both of which don't work.

两者都不起作用。

回答by Wilka

For all the standard library types the member function empty()is a query, not a command, i.e. it means "are you empty?" not "please throw away your contents".

对于所有标准库类型,成员函数empty()是一个查询,而不是一个命令,即它的意思是“你是空的吗?” 不是“请扔掉你的内容”。

The clear()member function is inherited from iosand is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit(end-of-file), then calling clear()will set the error state back to goodbit(no error).

clear()成员函数继承自ios并用于清除流的错误状态,例如,如果文件流的错误状态设置为eofbit(文件结束),则调用clear()会将错误状态设置回goodbit(无错误) .

For clearing the contents of a stringstream, using:

要清除 a 的内容stringstream,请使用:

m.str("");

is correct, although using:

是正确的,虽然使用:

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

is technically more efficient, because you avoid invoking the std::stringconstructor that takes const char*. But any compiler these days should be able to generate the same code in both cases - so I would just go with whatever is more readable.

从技术上讲更有效,因为您避免调用std::string采用const char*. 但是现在任何编译器都应该能够在这两种情况下生成相同的代码 - 所以我只会选择更具可读性的代码。

回答by Nikos Athanasiou

You can clear the error state and empty the stringstream all in one line

您可以在一行中清除错误状态并清空字符串流

std::stringstream().swap(m); // swap m with a default constructed stringstream

This effectively resets m to a default constructed state

这有效地将 m 重置为默认构造状态

回答by CodingWithoutComments

m.str("");

seems to work.

似乎工作。

回答by jerron

This should be the most reliable way regardless of the compiler:

无论编译器如何,这应该是最可靠的方法:

m=std::stringstream();

回答by TimoK

I am always scoping it:

我总是在界定范围:

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

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

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

回答by Francisco Cortes

my 2 cents:

我的 2 美分:

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.

这似乎在 xcode 和 dev-c++ 中对我有用,我有一个菜单形式的程序,如果按照用户的请求迭代执行,将填充一个 stringstream 变量,该变量在第一次代码将正常工作时运行但不会在用户下次运行相同的代码时清除 stringstream。但是下面的两行代码最终每次都在填充字符串变量之前清除了字符串流变量。(2 小时的反复试验和谷歌搜索),顺便说一句,单独使用每一行是行不通的。

//clear the stringstream variable

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

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

回答by Alcino Dall Igna Junior

It's a conceptual problem.

这是一个概念问题。

Stringstream is a stream, so its iterators are forward, cannot return. In an output stringstream, you need a flush() to reinitialize it, as in any other output stream.

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

回答by John

These do not discard the data in the stringstream in gnu c++

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

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

The following does empty the stringstream for me:

以下确实为我清空了字符串流:

    m.str().clear();