Why use iterators instead of array indices?

前端 未结 27 1887
萌比男神i
萌比男神i 2020-11-22 15:45

Take the following two lines of code:

for (int i = 0; i < some_vector.size(); i++)
{
    //do stuff
}

And this:

for (som         


        
相关标签:
27条回答
  • 2020-11-22 16:17

    I probably should point out you can also call

    std::for_each(some_vector.begin(), some_vector.end(), &do_stuff);

    0 讨论(0)
  • 2020-11-22 16:17
    • If you like being close to the metal / don't trust their implementation details, don't use iterators.
    • If you regularly switch out one collection type for another during development, use iterators.
    • If you find it difficult to remember how to iterate different sorts of collections (maybe you have several types from several different external sources in use), use iterators to unify the means by which you walk over elements. This applies to say switching a linked list with an array list.

    Really, that's all there is to it. It's not as if you're going to gain more brevity either way on average, and if brevity really is your goal, you can always fall back on macros.

    0 讨论(0)
  • 2020-11-22 16:18

    Because it is more object-oriented. if you are iterating with an index you are assuming:

    a) that those objects are ordered
    b) that those objects can be obtained by an index
    c) that the index increment will hit every item
    d) that that index starts at zero

    With an iterator, you are saying "give me everything so I can work with it" without knowing what the underlying implementation is. (In Java, there are collections that cannot be accessed through an index)

    Also, with an iterator, no need to worry about going out of bounds of the array.

    0 讨论(0)
  • 2020-11-22 16:18

    During iteration you don't need to know number of item to be processed. You just need the item and iterators do such things very good.

    0 讨论(0)
  • 2020-11-22 16:20

    Imagine some_vector is implemented with a linked-list. Then requesting an item in the i-th place requires i operations to be done to traverse the list of nodes. Now, if you use iterator, generally speaking, it will make its best effort to be as efficient as possible (in the case of a linked list, it will maintain a pointer to the current node and advance it in each iteration, requiring just a single operation).

    So it provides two things:

    • Abstraction of use: you just want to iterate some elements, you don't care about how to do it
    • Performance
    0 讨论(0)
  • 2020-11-22 16:21

    I'm going to be the devils advocate here, and not recommend iterators. The main reason why, is all the source code I've worked on from Desktop application development to game development have i nor have i needed to use iterators. All the time they have not been required and secondly the hidden assumptions and code mess and debugging nightmares you get with iterators make them a prime example not to use it in any applications that require speed.

    Even from a maintence stand point they're a mess. Its not because of them but because of all the aliasing that happen behind the scene. How do i know that you haven't implemented your own virtual vector or array list that does something completely different to the standards. Do i know what type is currently now during runtime? Did you overload a operator I didn't have time to check all your source code. Hell do i even know what version of the STL your using?

    The next problem you got with iterators is leaky abstraction, though there are numerous web sites that discuss this in detail with them.

    Sorry, I have not and still have not seen any point in iterators. If they abstract the list or vector away from you, when in fact you should know already what vector or list your dealing with if you don't then your just going to be setting yourself up for some great debugging sessions in the future.

    0 讨论(0)
提交回复
热议问题