Const Iterators


The only way to iterate through a const collection is to use a const iterator. The * operator of const iterators returns a const reference to its associated item, thus preventing accidental modification of the underlying collection. The following example illustrates the use of a const iterator.

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

#include <iostream>
#include <vector>

vector< char* > v; // Vector of character strings.

void
print( const vector< char* >& cv )
  {
  vector< char* >::const_iterator i;
  for ( i = cv.begin(); i != cv.end(); ++i )
    cout << *i << "\n";
  }

void
main()
  {
  v.push_back( "zippy" );
  v.push_back( "motorboy" );
  print( v );
  }

zippy
motorboy

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