为什么是“!=”用于迭代器? [英] Why is "!=" used with iterators?

查看:107
本文介绍了为什么是“!=”用于迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于这样写循环:

for (std::size_t Index = 0; Index < Foo.Size(); Index++)
{
    // Do stuff with Foo[Index].
}

但是当我在其他代码中看到迭代器循环时,

But when I see iterator loops in others' code, they look like this:

for (Bar::Iterator Iterator = Foo.Begin(); Iterator != Foo.End(); Foo++)
{
    // Do stuff with *Iterator.
}



我找到 Iterator!= Foo.End )以进行输出。如果 Iterator 增加了多个,也可能很危险。

I find the Iterator != Foo.End() to be offputting. It can also be dangerous if Iterator is incremented by more than one.

似乎更正确 Iterator< Foo.End(),但我从来没有看到在真正的代码。为什么不呢?

It seems more "correct" to use Iterator < Foo.End(), but I never see that in real code. Why not?

推荐答案

所有迭代器都是相等的。只有随机存取迭代器在关系上是可比较的。

All iterators are equality comparable. Only random access iterators are relationally comparable. Input iterators, forward iterators, and bidirectional iterators are not relationally comparable.

因此,使用!= 的比较是比使用< 的比较更通用和灵活。

Thus, the comparison using != is more generic and flexible than the comparison using <.

有不同类型的迭代器,因为不是所有元素范围都具有相同的访问属性。例如,如果你有一个迭代器到一个数组(一个连续的元素序列)中,则

There are different categories of iterators because not all ranges of elements have the same access properties. For example,


  • ;你只需要比较指向元素的索引(或者指向它们的指针,因为迭代器可能只包含指向元素的指针);

  • if you have an iterators into an array (a contiguous sequence of elements), it's trivial to relationally compare them; you just have to compare the indices of the pointed to elements (or the pointers to them, since the iterators likely just contain pointers to the elements);

if你有迭代器到一个链表,你想测试一个迭代器是否小于另一个迭代器,你必须从一个迭代器中遍历链表的节点,直到你到达另一个迭代器,或者到达结束

if you have iterators into a linked list and you want to test whether one iterator is "less than" another iterator, you have to walk the nodes of the linked list from the one iterator until either you reach the other iterator or you reach the end of the list.

规则是迭代器上的所有操作都应该具有固定的时间复杂性,子线性时间复杂度)。您可以始终在常量时间执行等式比较,因为您只需要比较迭代器是否指向同一个对象。因此,所有的迭代器都是相等的。

The rule is that all operations on an iterator should have constant time complexity (or, at a minimum, sublinear time complexity). You can always perform an equality comparison in constant time since you just have to compare whether the iterators point to the same object. So, all iterators are equality comparable.

此外,不允许迭代器递增范围。所以,如果你最终在一个场景 it!= foo.end()不做与 it< foo.end(),你已经有了未定义的行为,因为你已经超过了范围的结束。

Further, you aren't allowed to increment an iterator past the end of the range into which it points. So, if you end up in a scenario where it != foo.end() does not do the same thing as it < foo.end(), you already have undefined behavior because you've iterated past the end of the range.

指向数组的指针:不允许将指针递增到超过数组的末尾;这样做的程序展示未定义的行为。 (对于索引显然不是这样,因为索引只是整数。)

The same is true for pointers into an array: you aren't allowed to increment a pointer beyond one-past-the-end of the array; a program that does so exhibits undefined behavior. (The same is obviously not true for indices, since indices are just integers.)

某些标准库实现(如Visual C ++标准库实现)当你使用这样的迭代器做非法操作时,会引发一个断言。

Some Standard Library implementations (like the Visual C++ Standard Library implementation) have helpful debug code that will raise an assertion when you do something illegal with an iterator like this.

这篇关于为什么是“!=”用于迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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