Iterators

Containers such as lists and maps do not behave like arrays, so you can't use a for loop to go through the elements in them. Likewise, because these containers are not accessible randomly, you cannot use a simple integer index. You can use iterators to refer to elements of a container.

Iterating through a Container

Each container type has a distinct iterator associated with it. For instance, this is how you declare the iterator for list<int>:

;> list<int>::iterator ili;

You have previously seen the operator :: in two forms: as the global scope operator (where it has one operand) and in the constant ios::app. Each container type has a scope that contains a type name (iterator), and the scope operator (::) allows you to access that ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.