什么是“字符串”、“流”? 和“stringstream” C++ 中的类?
我想知道c++中string和stream有什么区别,stringstream是什么?
I want to know what's the difference between string and stream in c++, and what's stringstream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
非常非正式地:字符串是字符的集合,流是操纵移动数据的工具。 字符串流是一个 C++ 类,可让您使用字符串作为流的数据源和目标。
Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.
istream
和ostream
:流数据接口(文件、套接字等)istringstream
:包装字符串并提供其内容的istream
ostringstream
:保存写入内容的ostream
它作为字符串示例:
istream
andostream
: interfaces to streaming data (files, sockets, etc.)istringstream
: anistream
that wraps a string and offers its contentsostringstream
: anostream
that saves the content written to it as a stringExample:
我的猜测是,字符串流就像 iostream,但不是写入或读取文件,而是写入或读取字符串。
My guess is that a stringstream is like an iostream, but instead of writing to or reading from a file, you write to or read from a string.
在 C 和/或 Unix 中,基本的隐喻是文件。 标准输出、标准输入、网络套接字均使用文件描述符表示。 因此,您可以使用
fprintf()
写入这些“文件”,而无需了解底层的实际内容。作为一种更安全、更酷的替代方案,C++ 提出了 iostream 作为几乎内置于其中的基本隐喻使用
<<
运算符的语言。 同样,文件、字符串和(带有库)网络可以使用流进行访问,而无需知道它们是什么。In C and/or Unix, the basic metaphor was the file. Standard out, standard in, network sockets were all represented using file descriptors. Thus you can use
fprintf()
to write into these "files" without knowing what's really underneath.As a safer and cooler alternative, C++ presented iostream as the basic metaphor which is almost built into the language using
<<
operator. Again, files, strings and (with library) network can be accessed using streams without knowing what it is.