next up previous contents index
Next: The LEDA Tools for Up: Basics Previous: Items

   
Iteration

 For many (container) types LEDA provides iteration macros. These macros can be used to iterate over the elements of lists, sets and dictionaries or the nodes and edges of a graph. Iteration macros can be used similarly to the C++ for statement. Examples are

PLEASE NOTE:
Inside the body of a forall loop insertions into or deletions from the corresponding container are not allowed, with one exception, the current item or object of the iteration may be removed, as in

forall_edges(e,G) {
  if (source(e) == target(e)) G.del_edge(e);
} // remove selfloops

The item based data types list, array, and dictionary provide now also an STL compatible iteration scheme. The following example shows STL iteration on lists. Note that not all LEDA supported compilers allow the usage of this feature. To enable STL iterators use the compile flag -DLEDA_STL_ITERATORS.   

list<int> L;
// fill list somehow
list<int>::iterator it;
for ( it = L.begin(); it != L.end(); it++ )
  cout << *it << endl;
list<int>::iterator defines the iterator type, begin() delivers access to the first list item via an iterator. end() is the past the end iterator and serves as an end marker. The increment operator ++ moves the iterator one position to the next item, and *it delivers the content of the item to which the iterator is pointing. For more information on STL please refer to the standard literature about STL.

For a more flexible access to the LEDA graph data type there are graph iterators which extent the STL paradigm to more complex container types. To make use of these features please refer to Graph Iterators.  


next up previous contents index
Next: The LEDA Tools for Up: Basics Previous: Items
LEDA research project
1999-04-23