流是什么意思?其特点是什么?
C++ 和 C# 都使用单词 stream
来命名许多类。
- C++:
iostream
、istream
、ostream
、stringstream
、ostream_iterator< /code>、
istream_iterator
... - C#:
Stream
、FileStream
、MemoryStream
,BufferedStream
...
所以这让我很好奇,stream
是什么意思? 流
有哪些特征? 我什么时候可以使用这个术语来命名我的类? 这仅限于文件 I/O 类吗?
有趣的是,据我所知,C 在任何地方都没有使用这个词。
C++ and C# both use the word stream
to name many classes.
- C++:
iostream
,istream
,ostream
,stringstream
,ostream_iterator
,istream_iterator
... - C#:
Stream
,FileStream
,MemoryStream
,BufferedStream
...
So it made me curious to know, what does stream
mean?
What are the characteristics of a stream
?
When can I use this term to name my classes?
Is this limited to file I/O classes only?
Interestingly, C doesn’t use this word anywhere, as far as I know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
许多数据结构(列表、集合等)充当容器 - 它们保存一组对象。但不是溪流;如果列表是一个桶,那么流就是一根软管。您可以从流中提取数据,或将数据推送到流中 - 但通常仅一次并且仅在一个方向(当然也有例外)。例如,网络上的 TCP 数据是流;您可以发送(或接收)数据块,但只能与另一台计算机连接,并且通常只能发送一次 - 您无法倒带互联网。
流还可以操纵通过它们的数据;压缩流、加密流等。但同样,这里的底层隐喻是数据流。文件通常也作为流来访问(在某种程度上);您可以访问顺序数据块。当然,大多数文件系统也提供随机访问,因此流确实提供了诸如查找、位置、长度等功能,但并非所有实现都支持此类功能。寻找某些流或获取打开套接字的长度没有任何意义。
Many data-structures (lists, collections, etc) act as containers - they hold a set of objects. But not a stream; if a list is a bucket, then a stream is a hose. You can pull data from a stream, or push data into a stream - but normally only once and only in one direction (there are exceptions of course). For example, TCP data over a network is a stream; you can send (or receive) chunks of data, but only in connection with the other computer, and usually only once - you can't rewind the Internet.
Streams can also manipulate data passing through them; compression streams, encryption streams, etc. But again - the underlying metaphor here is a hose of data. A file is also generally accessed (at some level) as a stream; you can access blocks of sequential data. Of course, most file systems also provide random access, so streams do offer things like Seek, Position, Length etc - but not all implementations support such. It has no meaning to seek some streams, or get the length of an open socket.
有几个不同的含义。 #1 可能是您的意思,但您可能也想看看#2。
在像您提到的库中,“流”只是“二进制数据”的抽象,它可能是也可能不是随机访问的(与连续生成的数据相反,例如您正在编写生成随机数据的流),或者可以存储在任何地方(RAM、硬盘、网络、用户大脑等)。它们很有用,因为它们可以让您避免细节,并编写不关心流的特定源的通用代码。
作为一个更通用的计算机科学概念,“流”有时被(宽松地)视为“有限或无限量的数据”。如果没有示例,这个概念有点难以解释,但在函数式编程中(如在Scheme中),您可以通过将对象的历史视为更改的“流”,将具有状态的对象转换为无状态对象。 (这个想法是,对象的状态可能会随着时间的推移而改变,但如果你将对象的整个生命周期视为变化的“流”,则流作为一个整体永远不会改变,你可以用它进行函数式编程。)
There's a couple different meanings. #1 is what you probably mean, but you might want to look at #2 too.
In the libraries like those you mentioned, a "stream" is just an abstraction for "binary data", that may or may not be random-access (as opposed to data that is continuously generated, such as if you were writing a stream that generated random data), or that may be stored anywhere (in RAM, on the hard disk, over a network, in the user's brain, etc.). They're useful because they let you avoid the details, and write generic code that doesn't care about the particular source of the stream.
As a more general computer science concept, a "stream" is sometimes thought of (loosely) as "finite or infinite amount of data". The concept is a bit difficult to explain without an example, but in functional programming (like in Scheme), you can turn a an object with state into a stateless object, by treating the object's history as a "stream" of changes. (The idea is that an object's state may change over time, but if you treat the object's entire life as a "stream" of changes, the stream as a whole never changes, and you can do functional programming with it.)
来自I/O Streams(尽管在java中,含义是C++ / C# 中相同)
在 C# 中,您提到的流派生自抽象基类 Stream 。该基类的每个实现都有特定的目的。
例如,FileStream 支持对文件,而 MemoryStream 在内存中工作流对象。与
FileStream
和MemoryStream
类不同,BufferedStream 类允许用户缓冲 I/O。除了上述类之外,还有其他几个类实现了 Stream 类。有关完整列表,请参阅 MSDN 文档<代码>流类。
From I/O Streams (though in java, the meaning is the same in C++ / C#)
In C#, the streams you have mentioned derive from the abstract base class Stream. Each implementation of this base class has a specific purpose.
For example, FileStream supports read / write operations on a file, while the MemoryStream works on an in-memory stream object. Unlike the
FileStream
andMemoryStream
classes, BufferedStream class allows the user to buffer the I/O.In addition to the above classes, there are several other classes that implement the
Stream
class. For a complete list, refer the MSDN documentation onStream
class.除了官方术语和解释之外,流这个词本身取自“现实生活”流 - 数据不是水,而是从一个地方传输到另一个地方。
关于您提出的问题但仍未得到答复,您可以在包含流的名称中命名您自己的类,但只有当您实现某种新流时,它才会具有正确的含义。
Official terms and explanations aside, the word stream itself was taken from the "real life" stream - instead of water, data is transferred from one place to another.
Regarding question you asked and still wasn't ansewered, you can name your own classes in names that contain stream but only if you implement some sort of new stream it will have correct meaning.
在
中定义的 C 函数对流进行操作。中的第 7.19.2 节流除了“字符的有序序列”之外,C99 讨论了它们的行为方式,但没有讨论它们是什么。
基本原理提供了更多上下文相应的部分,从以下内容开始:
这就是这个概念的由来。
In C functions defined in
<stdio.h>
operate on streams.Section 7.19.2 Streams in C99 discusses how they behave, though not what they are, apart from "an ordered sequence of characters".
The rationale gives more context in the corresponding section, starting with:
So that's where the concept comes from.