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?