简体   繁体   English

* it ++如何对输出迭代器有效?

[英]How is *it++ valid for output iterators?

In example code, I often see code such as *it++ for output iterators. 在示例代码中,我经常看到诸如*it++用于输出迭代器。 The expression *it++ makes a copy of it , increments it , and then returns the copy which is finally dereferenced. 表达*it++使得副本it ,递增it ,然后返回其最后取消引用的副本。 As I understand it, making a copy of an output iterator invalidates the source. 据我了解,制作输出迭代器的副本会使源无效。 But then the increment of it that is performed after creating the copy would be illegal, right? 但随后的增量it被创建副本后,执行将是非法的,对不对? Is my understanding of output iterators flawed? 我对输出迭代器的理解有缺陷吗?

The standard requires that *r++ = t work for output iterators (24.1.2). 该标准要求*r++ = t适用于输出迭代器(24.1.2)。 If it doesn't work, it's not an output iterator by the standard's definition. 如果它不起作用,它不是标准定义的输出迭代器。

It is up to the iterator implementation to make sure such statements work correctly under the hood. 由迭代器实现来确保这些语句在引擎盖下正确工作。

The reason that you shouldn't keep multiple copies of an output iterator is that it has single pass semantics. 你不应该保留输出迭代器的多个副本的原因是它具有单通道语义。 The iterator can only be dereferenced once at each value (ie it has to be incremented between each dereference operation). 迭代器只能在每个值处解除引用一次(即必须在每个解除引用操作之间递增)。 Once an iterator is dereferenced, a copy of it cannot be. 解除引用迭代器后,它的副本不能。

This is why *r++ = t works. 这就是*r++ = t原因。 A copy is made of the original iterator, the original iterator is dereferenced and the copy is incremented. 副本由原始迭代器构成,原始迭代器被解除引用并且副本递增。 The original iterator will never be used again, and the copy no longer references the same value. 永远不会再使用原始迭代器,并且副本不再引用相同的值。

The expression *it++ does not (have to) make a copy of it, does not increment it, etc. This expression is valid only for convenience, as it follows the usual semantics. 表达*it++ (必须)使它的一个副本, 增加它等,这表达式是有效的仅仅是为了方便,因为它遵循通常的语义。 Only operator= does the actual job. 只有operator=才能完成实际工作。 For example, in g++ implementation of ostream_iterator , operator* , operator++ and operator++(int) do only one thing: return *this (in other words, nothing!). 例如,在ostream_iterator g ++实现中, operator*operator++operator++(int)只做件事: return *this (换句话说,什么都没有!)。 We could write for example: 我们可以写例如:

it = 1;
it = 2;
*it = 3;
++it = 4;

Instead of: *it++ = 1; *it++ = 2; *it++ = 3; *it++ = 4; 而不是: *it++ = 1; *it++ = 2; *it++ = 3; *it++ = 4; *it++ = 1; *it++ = 2; *it++ = 3; *it++ = 4;

Output iterators just don't work like normal iterators and their interface is specified so that they can be used in pointer-like expressions ( *it++ = x ) with useful results. 输出迭代器不像普通迭代器那样工作,并且它们的接口被指定,以便它们可以在类似指针的表达式( *it++ = x )中使用,并且具有有用的结果。

Typically, operator*() , operator++() and operator++(int) all return *this as a reference and output iterators have a magic operator= which performs the expected output operation. 通常, operator*()operator++()operator++(int)都返回*this作为引用,输出迭代器有一个magic operator= ,它执行预期的输出操作。 Because you can't read from an output iterator, the fact that operator*() etc., don't work as for other iterators doesn't matter. 因为你无法从输出迭代器中读取,所以operator*()等不能像其他迭代器一样工作并不重要。

Looking at your comment, it seems that most of the confusion arises from the SGI documentation, which I'd say is a bit misleading on this point. 看看你的评论,似乎大多数混淆都来自SGI文档,我认为这在这一点上有点误导。

Copying an output iterator does not invalidate the copied iterator. 复制一个输出迭代器失效复制的迭代器。 The real limitation is pretty simple: you should only dereference a given value of output iterator once. 真正的限制很简单:你应该只取消引用一次输出迭代器的给定值。 Having two copies at a time, however, is fine as long as you only dereference once of them while they have the same value. 但是,一次拥有两个副本就没问题,只要你只有一个副本,它们具有相同的值。 In a case like there where you're dereferencing one, then throwing away its value, and incrementing the other but only dereferencing it after the increment has happened, it's all perfectly fine. 在这种情况下你要解除引用一个,然后丢弃它的值,并递增另一个但只在增量​​发生后才解除引用它,这一切都很好。

Isn't an iterator just a pointer? 迭代器不是指针吗? Incrementing, then dereferencing it just moves on to the next element. 递增,然后取消引用它只是移动到下一个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM