0

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().

Community
  • 1
  • 1
Eric Z
  • 14,327
  • 7
  • 45
  • 69
  • @David - maybe. Undefined behavior can do just about anything, including throwing some kind of exception. – Pete Becker Jan 22 '13 at 01:46
  • How did you get this iterator that might not be valid? In general, it's much easier to ensure that you don't create bad iterators than to test for them later. – Pete Becker Jan 22 '13 at 01:47
  • @Pete, I can ensure it's valid outside, that's why I assert the validation here. – Eric Z Jan 22 '13 at 01:51
  • 2
    The best you can do is iterate through the vector testing for equality to any of the iterators to objects within the vector. – David Schwartz Jan 22 '13 at 01:51
  • 2
    Related questions: http://stackoverflow.com/questions/2062956/c-best-way-to-check-if-an-iterator-is-valid, http://stackoverflow.com/questions/824157/how-to-check-whether-stl-iterator-points-at-anything – jogojapan Jan 22 '13 at 01:52
  • @DavidSchwartz: If you have access to vector, you can compare `iter` to `begin()` and `end()` using `<`. Vector's memory is contigous. – rburny Jan 22 '13 at 01:52
  • @David, Agree. That's a portable way. – Eric Z Jan 22 '13 at 01:53
  • @Burny, I'm afraid it's not right. When it's valid, you can loop it over from `begin()` to `end()`. When it's singular however, you cannot compare it with `begin()` and `end()`. – Eric Z Jan 22 '13 at 01:55
  • @Sancho, compared to the `m_intVector` I gave in the example. – Eric Z Jan 22 '13 at 01:56
  • 1
    @jogojapan: Related? Duplicate. – Lightness Races in Orbit Jan 22 '13 at 01:59
  • @Non-StopTimeTravel Maybe... the thing is, one of the two seems to be interested in the specific situation when an iterator was valid, but the underlying container was updated. The other one seems to be mostly about `vector` and `find`. That's why I hesitate to vote to close as duplicate. – jogojapan Jan 22 '13 at 02:03
  • @jogojapan: They're asking for precisely the same feature, irrespective of what they're going to go ahead and use it for. – Lightness Races in Orbit Jan 22 '13 at 02:09

2 Answers2

6

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).

rburny
  • 1,559
  • 1
  • 9
  • 28
0

> I mean it points to a existing element inside the vector, e.g., from m_intVector.begin() to m_intVector.end().

Sure. Just iterate through the contents of m_vector, and compare each iterator to iter.

for ( std::vector<int>::iterator it = m_intVector.begin (); it != m_intVector.end (); ++it )
    if ( it == iter ) return true;
return false;

But I suspect that you won't want to pay for that kind of checking.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45