为什么是“!=”与迭代器一起使用而不是“<&quot ;? [英] Why is "!=" used with iterators instead of "<"?

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

问题描述

我习惯写这样的循环:

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 (Foo::Iterator iterator = foo.begin(); iterator != foo.end(); iterator++)
{
    // 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.

此外,不允许在超过结束时增加迭代器。它指向的范围。所以,如果你最终出现在的情况下,它!= foo.end()不同,它< 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.

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

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