问题
I have been asked this question which I do not really know why.
If you have a pointer int * x;
You can compare pointers with >
and <
because it stands for the memory location something like 0x0000 0x0004 0x0008
, etc. I know iterators and pointers are different, but they act in a very similar way.
For example:
vector<int> myVector;
for(int i = 1; i < 101; i++)
{
myVector.push_back(i);
}
vector<int>::iterator it = myVector.begin();
while(it != myVector.end()) //Why can't we write it < myVector.end()
{
cout << *it << endl;
it++;
}
Why can't we write it < myVector.end()
in the while
statement?
I know it has to do with no overloading in STL. However, writing &*it < &*myVector.end()
works because it gets the memory location which reveals say 0x0004 0x0008
, etc.
Why is this?
回答1:
operator<
and operator>
can only be used with RandomAccessIterator. But operator!=
could also be used with InputIterator, ForwardIterator and BidirectionalIterator. For your sample code, it != myVector.end()
and it < myVector.end()
have the same effect, but the former is more general, then the code also works with other iterators (e.g. iterators of std::list
or std::map
etc).
BTW: Your sample code will be fine to use operator<
or operator>
, since the iterator of std::vector is RandomAccessIterator
.
回答2:
std::vector::iterator
is a random access iterator, and you can certainly compare them with <
and >
.
However, only random access iterators can be compared using anything other than ==
and !=
. Bidirectional, forward, and input iterators only define the equality/inequality comparison operators.
A std::list::iterator
, for example is an iterator pointing to some unspecified member of a std::list
. In this case, there's just no meaning to any other kind of a comparison.
回答3:
The problem is that <
and >
cannot be always used with iterators, because only some kinds of iterators support such operations (namely, random access iterators and the like). On the other hand, comparison operations such as !=
is always available.
Now, why care about using <
and >
if !=
has the same effect and always works?
Suppose you have some generic code:
template <class It>
void foo(It begin, It end)
{
while (--end != begin)
apply(*begin);
}
The code will compile for pointers, for example, but it won't for myList.begin()
.
来源:https://stackoverflow.com/questions/38121032/in-c-why-cant-we-compare-iterators-using-and