Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++ iterators "safe"?

Tags:

c++

iterator

stl

I have read the documentation for C++ list iterators, but couldn't figure out one thing: are C++ iterators "safe"? I mean, does it stop incrementing once it reaches the last existing element in a list?

[]'s

like image 235
Mauren Avatar asked Nov 28 '22 22:11

Mauren


1 Answers

No, they're not "safe" in that sense. It is possible to increment an iterator past the end. For all the iterators in the standard library doing this will result in undefined behaviour. You could define your own iterators that do behave in a safe way instead if you wanted to.

like image 56
Mark Byers Avatar answered Dec 05 '22 16:12

Mark Byers