std :: distance是否在其范围内进行交互? [英] Does std::distance interate over its range?

查看:99
本文介绍了std :: distance是否在其范围内进行交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

24.1.1.3关于InputIterators的说法:

输入迭代器上的算法绝不应该尝试两次通过

相同的迭代器。它们应该是单通算法。


在24.3.4.4中总结了std :: distance的影响:

效果:返回增量或减量的数量需要从头到尾获得



后者的措辞似乎有些含糊不清。这是否意味着

它实际上执行必要的增量或减量,或者它是否b / b确定(通过一些未指定的方式)必要的数字,并且

返回它?


必须满足下列条件之一:

1)我们假设它确实执行了必要的增量,其中

case std :: distance在编写

InputIterators的算法时没用。如果你确定距离,你就不能再使用

确定距离的范围。

2)我们假设它确定距离而不执行

递增。我不能严格证明这一点,但我怀疑这个

真的无法实施。


除非我怀疑#2是不正确,似乎只应为ForwardIterators定义std :: distance

。无论哪种方式,24.3.4.4的措辞

是不够的。


-

Alan Johnson

24.1.1.3 says about InputIterators:
Algorithms on input iterators should never attempt to pass through the
same iterator twice. They should be single pass algorithms.

In 24.3.4.4 summarizes the effects of std::distance as:
Effects: Returns the number of increments or decrements needed to get
from first to last.

The wording of the latter seems somewhat ambiguous. Does it mean that
it actually performs the necessary increments or decrements, or that it
determines (by some unspecified means) the necessary number, and
returns it?

One of the following must then be true:
1) We assume that it does perform the necessary increments, in which
case std::distance is useless when writing algorithms for
InputIterators. If you determine the distance, you can no longer use
the range for which the distance was determined.
2) We assume that it determines the distance without performing the
increments. I can''t prove it rigorously, but I suspect that this
really isn''t possible to implement.

Unless my suspicion in #2 is incorrect, it seems that std::distance
should only be defined for ForwardIterators. Either way, the wording
for 24.3.4.4 is insufficient.

--
Alan Johnson

推荐答案


Alan Johnson写道:
Alan Johnson wrote:



< snip>

<snip>


2)我们假设它确定距离而不执行

增量。我不能严格证明这一点,但我怀疑这个

真的无法实现。
2) We assume that it determines the distance without performing the
increments. I can''t prove it rigorously, but I suspect that this
really isn''t possible to implement.



对于随机访问迭代器,你不需要迭代计算

距离。

- -

JE

For random access iterators, you don''t need to iterate to calculate
distance.
- -
JE




JE写道:

JE wrote:

Alan Johnson写道:
Alan Johnson wrote:



< snip>

<snip>


2)我们假设它确定距离而不执行

增量。我不能严格证明这一点,但我怀疑这个

真的无法实现。
2) We assume that it determines the distance without performing the
increments. I can''t prove it rigorously, but I suspect that this
really isn''t possible to implement.



对于随机访问迭代器,您不需要迭代计算

距离。


For random access iterators, you don''t need to iterate to calculate
distance.



是的,但这并没有证明在不执行增量的情况下实施

std :: distance的可能性。你可以将它专门用于

随机访问迭代器,但你仍然需要以某种方式处理输入迭代器

(没有任何改进)。


-

Alan Johnson

True, but that doesn''t prove the possibility of implementing
std::distance without performing increments. You can specialize it for
random access iterators, but you still have to handle input iterators
(without any refinements) in some manner.

--
Alan Johnson


Alan Johnson写道:
Alan Johnson wrote:

24.1.1.3关于InputIterators的说法:

输入迭代器上的算法绝不应该尝试两次通过

相同的迭代器。它们应该是单通算法。


在24.3.4.4中总结了std :: distance的影响:

效果:返回增量或减量的数量需要从头到尾获得



后者的措辞似乎有些含糊不清。这是否意味着

它实际上执行必要的增量或减量,或者它是否b / b确定(通过一些未指定的方式)必要的数字,并且

返回它?


必须满足下列条件之一:

1)我们假设它确实执行了必要的增量,其中

case std :: distance在编写

InputIterators的算法时没用。如果你确定距离,你就不能再使用

确定距离的范围。

2)我们假设它确定距离而不执行

递增。我不能严格证明这一点,但我怀疑这个

真的无法实施。


除非我怀疑#2是不正确,似乎只应为ForwardIterators定义std :: distance

。无论哪种方式,24.3.4.4的措辞

是不够的。
24.1.1.3 says about InputIterators:
Algorithms on input iterators should never attempt to pass through the
same iterator twice. They should be single pass algorithms.

In 24.3.4.4 summarizes the effects of std::distance as:
Effects: Returns the number of increments or decrements needed to get
from first to last.

The wording of the latter seems somewhat ambiguous. Does it mean that
it actually performs the necessary increments or decrements, or that it
determines (by some unspecified means) the necessary number, and
returns it?

One of the following must then be true:
1) We assume that it does perform the necessary increments, in which
case std::distance is useless when writing algorithms for
InputIterators. If you determine the distance, you can no longer use
the range for which the distance was determined.
2) We assume that it determines the distance without performing the
increments. I can''t prove it rigorously, but I suspect that this
really isn''t possible to implement.

Unless my suspicion in #2 is incorrect, it seems that std::distance
should only be defined for ForwardIterators. Either way, the wording
for 24.3.4.4 is insufficient.



我相信#1是正确的。但是,这并不意味着只应为ForwardIterators定义

std :: distance。很可能

是这两个迭代器之间所有人需要的距离是两个迭代器之间的距离,而std :: distance可以做到这一点。如果

在开始使用

元素之前需要大小的范围,那么无论如何你可能会有ForwardIterators。


Nate

I believe that #1 is correct. However, that doesn''t mean that
std::distance should only be defined for ForwardIterators. It may well
be the case that all one needs from an InputIterator range is the
distance between the two iterators, and std::distance can do that. If
you need the size of the range before you start doing something with the
elements, then you probably will have ForwardIterators anyway.

Nate


这篇关于std :: distance是否在其范围内进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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