What are Iterators, C++?

前端 未结 5 395
孤城傲影
孤城傲影 2020-12-16 12:09

What are Iterators in C++?

相关标签:
5条回答
  • 2020-12-16 12:38

    Though it initially seems fairly obvious, this is actually a rather deeper question than you may realize. Along with Paul McJones, Alexander Stepanov (designer of the original, for anybody who's not aware of that) recently released a book named Elements of Programming (aka EOP). The entirety of chapter six in that book is devoted specifically to iterators, and quite a bit of the rest of the book relates closely to iterators as well. Anybody who really wants to know iterators in full detail might consider reading this book.

    Warning: EOP is not for the faint of heart. It's relatively short (~260 pages), but quite dense. Speaking from experience, the early going is a bit disconcerting. My initial reaction to the first chapter was more or less "well, this is so obvious it's hardly worth reading. I did start programming before last week, after all!"

    Fortunately, I did look at the exercises, and tried to do a couple -- and even though I had thought of the subjects as obvious, the exercises demand rigorous proofs. It's a bit like being asked to prove (in a mathematical sense) that water is wet. You end up just about needing to read the chapter a couple of times just to get past your own preconceived notion that you already know the answers, so you can look at the real question -- what does "wet" really mean; what are the fundamental characteristics of "wetness"?

    0 讨论(0)
  • 2020-12-16 12:50

    They're a representation of a position within a sequence. On their own they're little more than curiosities, but when dereferenced they result in the value contained within the sequence at the position it represents.

    0 讨论(0)
  • 2020-12-16 12:52

    Iterators are a way of traversing a collection of objects. Typically, they allow you to access an STL (Standard Template Library) container sequentially in ways similar to accessing a classical C array with a pointer. To access an object through an iterator, you dereference it like a C pointer. To access the next object in a collection, you use the increment (++) operator. Some containers have multiple kinds of iterators that allow you to traverse the collection in different ways.

    0 讨论(0)
  • 2020-12-16 12:54

    http://en.wikipedia.org/wiki/Iterator

    Something that lets you go through everything in an array, one by one.

    In c++, i think you're talking about "for_each" ... As far as I know, C++ doesn't actually have "foreach" unlike languages like C#. However, the standard template library has it.

    0 讨论(0)
  • 2020-12-16 13:04

    From p. 80 of Accelerated C++:

    An iterator is a value that

    • Identifies a container and an element in the container
    • Lets us examine the value stored in that element
    • Provides operations for moving between elements in the container
    • Restricts the available operations in ways that correspond to what the container can handle efficiently
    0 讨论(0)
提交回复
热议问题