问题描述
我想知道,如何为STL迭代器建立等号(==)?
I was wondering, how is equality (==) established for STL iterators?Is it a simple pointer comparison (and thus based on addresses) or something more fancy?
如果我有两个不同的列表对象的两个迭代器,我比较他们,结果总是假的?
If I have two iterators from two different list objects and I compare them, will the result always be false?
如果我将有效值与超出范围的值进行比较怎么办?
What about if I compare a valid value with one that's out of range? Is that always false?
推荐答案
迭代器类可以定义重载的==操作符,如果他们想要的话。所以结果取决于 operator ==
的实现。
Iterator classes can define overloaded == operators, if they want. So the result depends on the implementation of operator==
.
你实际上不应该比较迭代器不同的容器。我认为一些调试STL实现会发出警告,如果你这样做,这将帮助你捕获这种错误使用的代码中的情况。
You're not really supposed to compare iterators from different containers. I think some debug STL implementations will signal a warning if you do this, which will help you catch cases of this erroneous usage in your code.
这篇关于如何建立STL迭代器的平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!