流是什么意思?其特点是什么?

发布于 2024-10-19 08:57:42 字数 579 浏览 4 评论 0原文

C++C# 都使用单词 stream 来命名许多类。

  • C++iostreamistreamostreamstringstreamostream_iterator< /code>、istream_iterator...
  • C#StreamFileStreamMemoryStreamBufferedStream...

所以这让我很好奇,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遥远的绿洲 2024-10-26 08:57:42

许多数据结构(列表、集合等)充当容器 - 它们保存一组对象。但不是溪流;如果列表是一个桶,那么流就是一根软管。您可以从流中提取数据,或将数据推送到流中 - 但通常仅一次并且仅在一个方向(当然也有例外)。例如,网络上的 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.

白日梦 2024-10-26 08:57:42

有几个不同的含义。 #1 可能是您的意思,但您可能也想看看#2。

  1. 在像您提到的库中,“流”只是“二进制数据”的抽象,它可能是也可能不是随机访问的(与连续生成的数据相反,例如您正在编写生成随机数据的流),或者可以存储在任何地方(RAM、硬盘、网络、用户大脑等)。它们很有用,因为它们可以让您避免细节,并编写不关心流的特定源的通用代码。

  2. 作为一个更通用的计算机科学概念,“流”有时被(宽松地)视为“有限或无限量的数据”。如果没有示例,这个概念有点难以解释,但在函数式编程中(如在Scheme中),您可以通过将对象的历史视为更改的“流”,将具有状态的对象转换为无状态对象。 (这个想法是,对象的状态可能会随着时间的推移而改变,但如果你将对象的整个生命周期视为变化的“流”,则流作为一个整体永远不会改变,你可以用它进行函数式编程。)

There's a couple different meanings. #1 is what you probably mean, but you might want to look at #2 too.

  1. 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.

  2. 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.)

断桥再见 2024-10-26 08:57:42

来自I/O Streams(尽管在java中,含义是C++ / C# 中相同)

I/O 流代表输入
源或输出目的地。一个
流可以代表许多不同的
各种来源和目的地,
包括磁盘文件、设备、其他
程序和内存阵列。

流支持多种不同类型
数据,包括简单字节,
原始数据类型,本地化
人物和物体。一些溪流
简单地传递数据;他人操纵
并以有用的方式转换数据。

无论他们内部如何运作,
所有流都呈现相同的简单
模型到使用它们的程序:A
流是数据序列。一个
程序使用输入流来读取
来自某个来源的数据,某处的一项
时间。

在 C# 中,您提到的流派生自抽象基类 Stream 。该基类的每个实现都有特定的目的。

例如,FileStream 支持对文件,而 MemoryStream 在内存中工作流对象。与 FileStreamMemoryStream 类不同,BufferedStream 类允许用户缓冲 I/O。

除了上述类之外,还有其他几个类实现了 Stream 类。有关完整列表,请参阅 MSDN 文档<代码>流类。

From I/O Streams (though in java, the meaning is the same in C++ / C#)

An I/O Stream represents an input
source or an output destination. A
stream can represent many different
kinds of sources and destinations,
including disk files, devices, other
programs, and memory arrays.

Streams support many different kinds
of data, including simple bytes,
primitive data types, localized
characters, and objects. Some streams
simply pass on data; others manipulate
and transform the data in useful ways.

No matter how they work internally,
all streams present the same simple
model to programs that use them: A
stream is a sequence of data. A
program uses an input stream to read
data from a source, one item at a
time.

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 and MemoryStream 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 on Stream class.

荆棘i 2024-10-26 08:57:42

除了官方术语和解释之外,流这个词本身取自“现实生活” - 数据不是水,而是从一个地方传输到另一个地方。

关于您提出的问题但仍未得到答复,您可以在包含流的名称中命名您自己的类,但只有当您实现某种新流时,它才会具有正确的含义。

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.

_蜘蛛 2024-10-26 08:57:42

中定义的 C 函数对流进行操作。

中的第 7.19.2 节除了“字符的有序序列”之外,C99 讨论了它们的行为方式,但没有讨论它们是什么。

基本原理提供了更多上下文相应的部分,从以下内容开始:

C 从它诞生的 UNIX 环境继承了文本流的概念。

这就是这个概念的由来。

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:

C inherited its notion of text streams from the UNIX environment in which it was born.

So that's where the concept comes from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文