How to check if stl.iterator points somewhere?

As it is possible to default-construct an stl-container iterator I'd like to know how to check, if the iterator points somewhere meaningful. Consider
1
2
std::map<int,double>::iterator it;
++it;

This segfaults. So is there a way of checking that it has been properly initialized?

mfg,J
closed account (EzwRko23)
No. Just like there is no method for checking whether a given pointer points to a valid object of desired type.
Last edited on
I will have to clarify the question:
I don't want to know if the iterator points to a valid item but if it points at all. To take up your parallel to pointers: with a pointer I CAN check whether it's NULL. I'm looking for somethinig similar for iterators
No.

You can assign it container.end() and check for that.

1
2
3
4
5
6
7
std::map<int, double> my_map;
std::map<int, double>::iterator my_iter( my_map.end() );

if( my_iter == my_map.end() )
{
   // ...
}
Ok thanks, that was what I did in the end. Just thought there might be something more specifically for that purpose.
Topic archived. No new replies allowed.