Are C ++ iterators "safe"?

I read the documentation for C ++ list iterators, but couldn't figure out what it is: Are C ++ iterators "safe"? I mean, does it stop growing as soon as it reaches the last existing item in the list?

[] 's

+3
source share
7 answers

No, they are not “safe” in that sense. You can increase the iterator to the end. For all iterators in the standard library, this will lead to undefined behavior. You could define your own iterators that will behave in a safe way if you want.

+11
source

, , std::list::iterator. - , . , , . , .

, , , .. . :

  • std::list, , erase(), , , .
  • std::remove() , , , .

, , , .

+9

, it != list.end(), , .

+3

. " ", Undefined Behavior.

+2

. ++, .

, , .

0

, "", , .

- . , , , . std (. Std:: copy (first_in, last_in, first_out, last_out) , , (, )

- , , . , ++, operator-- . , contianer . . - Iterator, Iterator Proxy

0

Use correctly, iterators are very useful.
Therefore, the question “C ++ Iterators are Safe” will depend on how it is used.
Please note: misuse of the printf API can lead to serious errors. The implementation will be safe to use or not.

-1
source

Source: https://habr.com/ru/post/1788039/


All Articles