是前向迭代器输出迭代器? [英] Are Forward-Iterators Output-Iterators?

查看:201
本文介绍了是前向迭代器输出迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ForwardIterator是否需要为OutputIterators?我当前的STL实现(VS2012)从 input_iterator_tag output_iterator_tag forward_iterator_tag c $ c>,但是我在标准[N3485]中找不到这个要求。

Are ForwardIterators required to be OutputIterators? My current STL-implementation (VS2012) derives forward_iterator_tag from both input_iterator_tag and output_iterator_tag, but I can't find this requirement in the standard [N3485].

推荐答案

,否,前向迭代器不需要是输出迭代器。输出迭代器需求就像迭代器可以具有的额外的一组需求,而不管其满足的迭代器的其余要求。前向迭代器只需要是输入迭代器(§24.2.5/ 1):

In C++11, no, forward iterators are not required to be output iterators. The output iterator requirements are like an extra set of requirements that an iterator can have, regardless of the rest of the iterator requirements it meets. Forward iterators are only required to be input iterators (§24.2.5/1):


类或指针类型X满足要求a forward iterator if:

A class or pointer type X satisfies the requirements of a forward iterator if:


  • X满足输入迭代器的要求

  • ...

事实上,正向迭代器满足输出迭代器需求,只有当它是一个可变迭代器

In fact, a forward iterator meets the output iterator requirements only if it is a mutable iterator to a sequence of copy-assignable types.

†或者一个常量迭代器到类型的序列。

更多的是,迭代器标签被定义为具有可变成员按标准(§24.4.3/ 2):

More to the point, the iterator tags are defined specifically by the standard as (§24.4.3/2):

namespace std {
  struct input_iterator_tag { };
  struct output_iterator_tag { };
  struct forward_iterator_tag: public input_iterator_tag { };
  struct bidirectional_iterator_tag: public forward_iterator_tag { };
  struct random_access_iterator_tag: public bidirectional_iterator_tag { };
}



如您所见, forward_iterator_tag 只能继承 input_iterator_tag

++ 03,它说明正向迭代器满足输入和输出迭代器的要求:

In C++03, it is stated that forward iterators satisfy the requirements of input and output iterators:


正向迭代器满足输入的所有要求

Forward iterators satisfy all the requirements of the input and output iterators and can be used whenever either kind is specified.

但是这在下面的段落中是矛盾的,前向迭代器不能满足输出迭代器的要求:

But this is then contradicted in the following paragraph, stating that a constant forward iterator would not satisfy the requirements for output iterators:


除了类别,前向,双向或随机访问迭代器也可以是可变的或常数,取决于表达式* i的结果是否作为引用或作为常量的引用。常量迭代器不满足输出迭代器的要求,表达式* i(对于常量迭代器i)的结果不能用于需要lvalue的表达式中。

Besides its category, a forward, bidirectional, or random access iterator can also be mutable or constant depending on whether the result of the expression *i behaves as a reference or as a reference to a constant. Constant iterators do not satisfy the requirements for output iterators, and the result of the expression *i (for constant iterator i) cannot be used in an expression where an lvalue is required.

但是,迭代器标签的定义与C ++ 11中的相同。对于这种矛盾的措辞,有缺陷报告,但它被关闭为非缺陷,因为第一个报价是在该部分的介绍性文本,并且可能会在未来(它是)重新编写。

However, the definition of the iterator tags is identical to as in C++11. There was a defect report for this contradictory wording, but it was closed as Not a Defect because the first quote is in the "introductory text" of the section and would likely be reworded in the future (which it was).

前向迭代器的SGI定义作为输入和输出迭代器的细化(感谢评论中的@BenVoigt)。

The SGI definition of a forward iterator is given as a refinement of both input and output iterators (thanks to @BenVoigt in the comments).

但是,如果我们看一下实现迭代器标签,我们发现 forward_iterator_tag 仍然只继承自 input_iterator_tag

Nonetheless, if we take a look at the implementation of the iterator tags, we find that forward_iterator_tag still only inherits from input_iterator_tag.

看起来这是一个在过去相当困惑的领域,但如果VS2012定义 forward_iterator_tag 作为继承输出_ - 和 input_iterator_tag 只是假设它是一个错误。

Looks like this has been an area of quite a bit of confusion in the past, but if VS2012 is defining forward_iterator_tag as inheriting from both output_- and input_iterator_tag, I can only assume it is a bug.

这篇关于是前向迭代器输出迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆