Why do I need to dereference iterators?

后端 未结 3 1799
小蘑菇
小蘑菇 2021-01-29 08:19

Why do I need to dereference iterators? For example in the following program

#include 
#include 
#include 

int main(         


        
3条回答
  •  庸人自扰
    2021-01-29 09:16

    You need to be able to perform operations on the iterator itself e.g. your code does it != s.end() and ++it

    And sometimes you need to be able to perform operations on the object the iterator points to, e.g. isupper(*it)

    There has to be some operation to indicate you want to refer to the thing it refers to not the iterator itself, otherwise if you test if (it == another_it) how would the compiler know if you were trying to compare the iterators or the things they point to? The same applies to the ++it operation, which wants to modify the iterator, not the thing it points to.

    Iterators could have a get() member to return the thing they refer to, so you'd do isupper(it.get()) but instead they support the dereference operator, which is less typing and is consistent with pointers and allows pointers to be used in any code that expects iterators.

提交回复
热议问题