I have a vector of IntRect: vector.
How can I iterate from both ends of the list and stop the iterator when the iterator intersects?
vector<IntRect>::iterator itr = myVector.begin();
vector<IntRect>::reverse_iterator revItr.rbegin();
for (; /*check itr and revItr does not intersect, and itr and revItr do not end */ ; ++itr, ++revItr) {
IntRect r1 = *itr;
IntRect r2 = *revItr;
// do something with r1 and r2
}
Thank you.
I think you need to check
empty()with that implementation – suspect thatend()-1isn’t defined if the vector is empty. I haven’t used it before, but Dinkumware STL at least has operator < defined for vector iterators and it appears to do something sensible.Also note that you need to check <, not just equality – that takes care of the (common) case where your vector has an even number of entries and the two iterators would step past one another.