C++ iterator of list [duplicate]
Possible Duplicate:
C++: Check if iterator belongs to a list
How can I check if an iterator recieved by function belongs to the right list?
List<double> doublesList;
List<double> doublesList2;
// Can't mix iterators
ASSERT_THROW(ListExceptions::ElementNotFound, doublesList.remove(doublesList2.begin()));
ASSERT_THROW(ListExceptions::ElementNotFound, doublesList2.insert(5.0, doublesList.begin()));
I askes for the case of my own Linked List and my own Iterator. Thanks a lot.
With standard containers, you can't. The C++ standard library leaves this up to the programmer. Comparison of iterators invokes UB, in fact.
With homegrown containers, you can insert information into the iterators so that they remember which container they belong to; that makes the iterators heavier, of course, so it's often only sensible in debug mode.
精彩评论