规范的方式定义正向输出迭代器 [英] Canonical way to define forward output iterator

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

问题描述

如何以规范的方式定义C ++ 11中的正向输出迭代器?

How does one define forward-output-iterators in C++11 in a canonical way?

根据标准,forward_iterator只是一个input_iterator。因此,相应的 forward_iterator_tag 只能扩展 input_iterator_tag 。如果我们使用 std :: iterator 来定义我们的迭代器,我们使用什么标签作为前向输出迭代器?

According to the standard a forward_iterator is only a input_iterator. So the corresponding forward_iterator_tag only extends input_iterator_tag. If we are using std::iterator to define our iterators, what tag do we use for a forward-output-iterator?

是否可以定义一个扩展 forward_iterator_tag output_iterator_tag 的私有标签,还是有更好的解决方案?

Is it canonical to define a private tag that extends both forward_iterator_tag and output_iterator_tag or is there a better solution?

推荐答案

规范的做法是继承 std :: iterator< std :: forward_iterator_tag ,T> 。迭代器只有一个类别。

The canonical thing to do is to inherit from std::iterator<std::forward_iterator_tag, T> only. Iterators have only one category.

标准没有用于输出迭代器(也是向前迭代器)的算法(或其他用法)。在标准中,输出迭代器的所有使用只需要单次传递。

The standard has no algorithms (or other uses) for an output iterator that is also a forward iterator. All uses of output iterators in the standard require only single-pass.

而是,标准具有类别forward / bidi / randomaccess的可变对不可变迭代器的想法。所有需要通过迭代器编写的算法,并且需要优于单遍的算法,也可以通过它们通过的相同迭代器来读取。这是 std :: remove std :: sort 和其他变异算法。

Instead, the standard has the idea of mutable vs. immutable iterators of categories forward/bidi/randomaccess. All the algorithms that need to write through iterators, and that require better than single-pass, also read through the same iterators they write through. This is std::remove, std::sort and other mutating algorithms.

可变和不可变迭代器之间的区别不是通过迭代器标签检测,它是由赋值表达式是否形式确定。因此,例如,如果你通过一个迭代器到 std :: sort 是不可变的,那么算法不会编译反正,所以一般不需要一个输入迭代器也是标记为 output_iterator_tag 。所有需要 OutputIterator 的算法将只使用一个可变的 ForwardIterator ,再也没有必要 output_iterator_tag

The difference between mutable and immutable iterators is not detected by iterator tag, it's determined by whether the assignment expressions are well-formed. So for example if you pass an iterator to std::sort that's immutable, then the algorithm won't compile anyway, so there's generally no need for an input iterator to also be tagged with output_iterator_tag. All algorithms that require an OutputIterator will Just Work with a mutable ForwardIterator, again there is no need for it to be tagged with output_iterator_tag.

如果你有不同于标准算法的需求,一个原因,你的建议不会为你的迭代器工作。但它不会检测可变标准迭代器。例如 std :: deque< int> :: iterator int * random_access_iterator_tag ,而不是您的私有标记,而不是与 output_iterator_tag 有关的任何内容。所以你可能会更好地定义自己的traits类,而不是希望适应现有的 iterator_traits :: iterator_category 来提供你想要的信息。

If you have different needs from those of the standard algorithms then I can't immediately think of a reason that your proposal won't work for your iterators. But it won't detect mutable standard iterators. For example std::deque<int>::iterator and int* have iterator category random_access_iterator_tag, not your private tag and not anything to do with output_iterator_tag. So you would probably be better off defining your own traits class rather than hoping to adapt the existing iterator_traits::iterator_category to provide the information you want.

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

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