tags:

views:

233

answers:

5

What are Iterators in C++?

+2  A: 

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.

ItzWarty
It is certainly more than arrays, in fact the very goal of the iterators in C++ is to abstract away the collection itself and its properties so that algorithms dealing with iterators can be used over any type of collection :)
Matthieu M.
A: 

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.

Ignacio Vazquez-Abrams
+7  A: 

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.

Splat
+7  A: 

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"?

Jerry Coffin
It's such a pleaseure to come on S.O to find answers like these. +1
lucifer
Thanks for the pointer to a book I should have known about (if not read) before.
Michael Burr
+2  A: 

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
Alexandros Gezerlis