我只是想了解一下 C++ 中的 InputIterator 和 OutputIterator 实际上是什么。现在,直到现在,我一直假设我一直知道的输入和输出的定义也适用于迭代器。
因此,这意味着Input是“放入某物中”,这意味着您可以向迭代器写入内容,就像将某物放入迭代器中一样。
输出是“放在某个地方”,这对我来说一直意味着写入屏幕或粘贴到变量中。
现在事实证明,我们的朋友迭代器,是另一种方式,一个 InputIterator 用于对元素序列进行只读访问,这似乎应该称为“输出”,如下所示例如,您可以使用这种类型的迭代器将 std::vector
的元素写入 std::cout
。
OutputIterator 结果是只写访问的。
我想知道,是否有人可以对此给出合理的解释,我发现迭代器类型的命名相当奇怪?
I am just trying to wrap my head around what an InputIterator and OutputIterator in C++ actually are. Now, until now, I always assumed that the definitions I have always known of Input and Output also applied to iterators.
So that means that Input is "to place into something", meaning you can write to the iterator, as in, put something in the iterator.
Output is "to put out somewhere" and that has always meant to me, to write to the screen or stick into a variable.
Now it turns out with our friends Iterators, it is the other way, an InputIterator is used for read-only access to a sequence of elements, which seems to be it should be called "Output", as you'd use this type of iterator to write the elements of a std::vector<int>
to std::cout
for example.
The OutputIterator turns out to be the one that is write-only access.
I wondered, if anyone can give a reasonable explanation for this, I find, rather odd, naming of iterator types?
发布评论
评论(2)
这只是一个惯例问题。 “输入”和“输出”被理解为“从”/“到”相对于环境,而不是相对于你的程序;或者在另一个方向上,作为相对于您的程序而不是环境的“进入/退出”。
因此,“输入”从环境中获取一些东西,并将其提供给您的程序,例如
istream
或read
。 “输出”将程序中的某些内容放回环境中,例如ostream
和write
。迭代器遵循相同的方向,输入迭代器使程序可以使用某个范围中的数据,而输出迭代器将程序中的数据放入某个范围中。
您可能会想,“输入”是您的程序应该写入的其他人的输入。但我猜这个措辞假定了一个以自我为中心的程序员,他们自己的程序是参考点,而不是环境中的任何人(或每个人)。
It's just a matter of convention. "Input" and "output" are understood as "from"/"to" with respect to the environment, not with respect to your program; or in the other direction, as "into/out of" with respect to your program, not the environment.
So "input" gets something from the environment and makes it available to your program, like
istream
orread
. "Output" puts something from your program back out into the environment, likeostream
andwrite
.The iterators follow the same direction, an input iterator makes the data from a range available to your program, while the output iterator puts data from your program into a range.
The way you may be thinking is that "input" is somebody else's input that your program should want to write to. But I guess the wording presumes an egocentric programmer for whom their own program is the point of reference, not anyone (or everyone) in the environment.
输入迭代器是输入流的包装器。
输出迭代器是输出流的包装器。
(诚然,您可以编写其他输入/输出迭代器,但这些是标准中存在的示例,以
istream_iterator
和ostream_iterator
的形式)输入流是一个您从中读取(获得输入),因此,输入迭代器也是您可以从中获取输入的迭代器。
我同意,如果您只将它们视为迭代器,这可能有点不直观。但是当您记住它们通常包装流时,这就更有意义了。
例如,您可以围绕
std::cout
创建一个ostream_iterator
(这是一个 OutputIterator)。An input iterator is a wrapper around an input stream.
An output iterator is a wrapper around an output stream.
(Admittedly, you can write other input/output iterators, but those are the examples that exist in the standard, in the form of
istream_iterator
andostream_iterator
)An input stream is one you read from (you get input), and thus, an input iterator is one you can get input from as well.
I agree, it can be somewhat unintuitive if you just consider them as iterators. But when you remember that they typically wrap a stream, it makes more sense.
You can create an
ostream_iterator
(which is an OutputIterator) aroundstd::cout
, for example.