10.9. Container Classes and Iterators
Among the most popular types of classes
are container classes (also called collection classes), i.e., classes designed to hold collections of
objects. Container classes commonly provide services such as insertion,
deletion, searching, sorting, and testing an item to determine whether it is a
member of the collection. Arrays, stacks, queues, trees and linked lists are
examples of container classes; we studied arrays in Chapter
7 and will study several of these other data
structures in Chapter
20.
It is common to associate iterator objects—or more simply iterators—with container
classes. An iterator is an object that "walks through" a collection, returning
the next item (or performing some action on the next item). Once an iterator for
a class has been written, obtaining the next element from the class can be
expressed simply. Just as a book being shared by several people could have
several bookmarks in it at once, a container class can have several iterators
operating on it at once. Each iterator maintains its own "position" information.
We discuss containers and iterators in detail in Chapter
20.