23

Possible Duplicate:
C++ Best way to check if an iterator is valid

I want to do something like this:

std::vector<int>::iterator it;
// /cut/ search for something in vector and point iterator at it. 
if(!it) //check whether found
    do_something(); 

But there is no operator! for iterators. How can I check whether iterator points at anything?

Community
  • 1
  • 1
  • It doesn't make sense to use an iterator without any reference to the container it's iterating. See James Hopkin's answer. – Mark Ransom Jun 04 '09 at 18:39
  • clarifying the darkness Invalidation of STL Iterators: http://www.angelikalanger.com/Conferences/Slides/CppInvalidIterators-DevConnections-2002.pdf – lsalamon Mar 31 '10 at 17:12
  • Vote to reopen because: 1) target question is newer, so it should be the other a duplicate of this one. 2) The question is not the seame: this ask if the iterator is not the ending iterator, while the other ask for invalidated iterators. – Adrian Maire Apr 20 '18 at 08:45

4 Answers4

49

You can't. The usual idiom is to use the container's end iterator as a 'not found' marker. This is what std::find returns.

std::vector<int>::iterator i = std::find(v.begin(), v.end(), 13);
if (i != v.end())
{
     // ...
}

The only thing you can do with an unassigned iterator is assign a value to it.

James Hopkin
  • 13,797
  • 1
  • 42
  • 71
  • 1
    You covered the case when the iterator is initialized and still valid. However, some operations on some containers may invalidate the iterators. For example, removal of elements from a vector may invalidate iterators ( the iterator pointing to the last element in the vector in this case). – Cătălin Pitiș May 05 '09 at 10:55
  • 2
    @Cătălin: that's all true, but out of the scope of the question, I think. Just as the ! (`not`) operator will only work with valid or null pointers, the above will only work with valid iterators (including `end` iterators). – James Hopkin May 05 '09 at 11:12
  • Iterator validity is in the programmer's scope of things to worry about. YOU are the only that need to make sure you are not doing something nasty with your iterators. – Spidey Jul 04 '12 at 13:15
2

Though the iterators are considered as general form of pointers, they are not exactly the pointers. The standard defines Past-the-end iterator to indicate the search failure in containers. Hence, it is not recommended to check the iterators for NULL

Past-the-end values are nonsingular and nondereferenceable.

if(it != aVector.end())  //past-the-end iterator
    do_something();
aJ.
  • 34,624
  • 22
  • 86
  • 128
0

If you want to use iterator in a loop, the safest way to use it is in this fashion:

for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
 do_smth();
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Mikhail Aksenov
  • 944
  • 9
  • 23
-2

I believe this should generally give you a good test:

if (iterator._Mycont == &MyContainer)
{
Probably a valid iterator!
}

You could do tests to make sure that the iterator does not equal the end...

iterator != MyContainer.end()

and:

iterator >= MyContainer.begin()
Jason Plank
  • 2,336
  • 5
  • 31
  • 40