Does Using a Pointer as a Container Iterator Violate the Standard

前端 未结 3 1011
一向
一向 2021-01-12 20:34

Angew made a comment that a vector using a raw pointer as it\'s iterator type was fine. That kinda threw me for a loop.

I started researching it and fou

3条回答
  •  遥遥无期
    2021-01-12 21:01

    My 50 cents:

    Iterators are generic ways to access any STL container. What I feel you're saying is: Since pointers are OK as a replacement of iterators for vectors, why are there iterators for vectors?

    Well, who said you can't have duplicates in C++? Actually it's a good thing to have different interfaces to the same functionality. That should not be a problem.

    On the other hand, think about libraries that have algorithms that use iterators. If vectors don't have iterators, it's just an invitation to exceptions (exceptions in the linguistic since, not programming sense). Every time one has to write an algorithm, he must do something different for vectors with pointers. But why? No reason for this hassle. Just interface everything the same way.

提交回复
热议问题