Forward iterator may store and retrieve values

You're viewing an older version of this page (#4213). View the current version.

View versions (1)

Description

Forward iterators are made for sequential access for list, set, multiset, map, and multimap.

Forward iterators have all the abilities of input iterators and most of those of output iterators like in the table below.

Note! You can make multiple copies of a forward iterator, each of which can be dereferenced and incremented independently.

Operations

ExpressionDescription
*iProvides read access to the actual element
i->mProvides read access to a member of the actual element
++iSteps forward and returns new position (when not null)
i++Steps forward and returns old position (when not null)
i1==i2Returns if i1 is equal to i2
i1!=i2Returns if i1 is not equal to i2
TYPE()Creates iterator (default constructor)
TYPE(i)Copies iterator (copy constructor)
i1=i2Assigns an iterator

References

Example 1
Problem

This program illustrates a hash_set including a default constructor and the insert(), begin(), erase() member functions of the STL hash_set interface.

Workings
#define _DEFINE_DEPRECATED_HASH_CLASSES 0
#include <iostream>
#include <hash_set>

using namespace std;
using namespace stdext;

int main
{
  hash_set <int> hs1;
  hash_set <int>::iterator hs1_Iter;
  hash_set <int>::const_iterator hs1_cIter;

  hs1.insert( 1 );
  hs1.insert( 2 );
  hs1.insert( 3 );

  hs1_Iter = hs1.begin( );
  cout <<"The first element of hs1 is "<<*hs1_Iter<<endl;

  hs1_Iter = hs1.begin( );
  hs1.erase( hs1_Iter );

  // The following 2 lines would err because the iterator is const
  // hs1_cIter = hs1.begin( );
  // hs1.erase( hs1_cIter );

  hs1_cIter = hs1.begin( );
  cout <<"The first element of hs1 is now "<<*hs1_cIter<<endl;

  return 0;
}
Solution

Output:

The first element of hs1 is 1
The first element of hs1 is now 2

See Also