Using Basic Iterators


The type of iterator associated with a particular entity depends on how easily an iterator can move through the entity and whether the entity is readable and/or writeable. The following table shows the mapping between STL container types and their associated iterator types.
 
Container Iterator type Container Iterator type

vector

random access

map

bidirectional

deque

random access

multimap

bidirectional

list

bidirectional

stack

none

set

bidirectional

queue

none

multiset

bidirectional

priority_queue

none

Most of the time you do not have to remember this information, as each collection defines the following useful typedefs .
 
Typedef Meaning

iterator

Type that can iterate forward through a writeable collection

const_iterator

Type that can iterate forward through a read-only collection

reverse_iterator

Type that can iterate backward through a writeable collection

const_reverse_iterator

Type that can iterate backward through a read-only collection

For example, the following code declares an iterator iter1 that iterates through a vector of integers.

  vector< int >::iterator iter1;
  

Similarly, the following code declares an iterator iter2 that iterates backward through a list of integers.

  list< int >::reverse_iterator iter2;
  

Most containers provide the following set of functions for obtaining an iterator positioned at an extremity.

begin
iterator begin()
Returns an iterator positioned at the first item in the collection.
begin
const_iterator begin() const
Returns a const iterator positioned at the first item in the collection.
end
iterator end()
Returns an iterator positioned immediately after the last item in the collection.
end
const_iterator end() const
Returns a const iterator positioned immediately after the last item in the collection.

When an iterator is positioned inside its collection, it can be safely dereferenced. However, when an iterator goes beyond the last element of its collection, the iterator is set to a past-the-end value equal to end() , and any attempt to dereference it can cause a runtime error.

The following example shows how to use an iterator to display every element of a collection. The example uses the ++ operator to advance the iterator, the * operator to access the iterator's associated element, and the != operator to compare the position of the iterator against its past-the-end value.

Example <ospace/osstd/examples/iter1.cpp>

#include <iostream>
#include <vector>

void
main()
  {
  vector< char* > v; // Vector of character strings.
  v.push_back( "zippy" );
  v.push_back( "motorboy" );

  vector< char* >::iterator i = v.begin();
  for ( i = v.begin(); i != v.end(); ++i )
    cout << *i << "\n";
  }

zippy
motorboy

Because most containers support iterator access, you can use the same technique for iterating through any kind of container, including sets and maps.

The preferred method for iteration through a container is to always use the != operator for past-the-end checking, as shown in the previous example. Although the following code segment can be used to perform the loop in the previous example, this approach is not recommended.

// OK for collections that have random access iterators,
// but not recommended.
  for ( i = v.begin(); i < v.end(); i++ )
  cout << *i << endl; // Display item.
  

The code segment above performs correctly only with random access iterator containers. Random access iterators are the only iterators to supply a < operator.


Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.