How to check if the iterator is initialized?

前端 未结 11 1345
孤城傲影
孤城傲影 2021-02-05 06:30

If I use a default constructor for an iterator, how to check if it was assigned later on?

For pointers, I could do this :

int *p = NULL;
/// some code
         


        
11条回答
  •  -上瘾入骨i
    2021-02-05 07:14

    In C++, uninitialized local variables can have any value i.e it contains simply garbage. That implies, you cannot check it against some well-defined value, to determine if the variable is uninitialized or not.

    Not only that if the variable is not initialized and you write this:

    if ( NULL == it ) // this fails
    

    then it invokes undefined behavior.

提交回复
热议问题