I. Examples
We know that Iterator is provided in STL to traverse the Vector or List data structure.
The Iterator mode is used to solve the traversal problem of an aggregate object. It encapsulates the traversal of the aggregate into a class, so as to avoid exposing the internal representation of the aggregate object.
For example, in STL, the following structures are available:
Ii. iterator Mode
Definition: provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.
The typical example is the for_each operation in STL:
[Cpp] // function called for each element
Void print (int elem)
{
Cout <elem <'';
}
Int main ()
{
Vector <int> coll;
INSERT_ELEMENTS (coll, 1, 9 );
// For_each will call print (elem) for each elem)
For_each (coll. begin (), coll. end (), // range
Print); // operation
Cout <endl;
}
Author lwbeyond