Possible Duplicate:
C++ Best way to check if an iterator is valid
Let’s say I have a function which takes an iterator as its sole parameter as below.
void DoSomethingWithIterator(std::vector<int>::iterator iter)
{
// Check the pre-condition
assert( /* how to validate iter here? */ )
// Operate on iter afterwards
..
}
How do I know if iter is valid or not. By valid, I mean it points to a existing element inside the vector, e.g., from m_intVector.begin() to m_intVector.end().
In general, you can’t do that. C++ types are designed to be maximally efficient and don’t contain such additional information.
For example, vector’s iterator is likely to be equivalent to a pointer to element (this is the case on my machine, using g++ 4.7.2).