next up previous contents index
Next: Modules Up: Basics Previous: Items   Contents   Index


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 self-loops

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.

using namespace leda;
using std::cin;
using std::cout;
using std::endl;

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: Modules Up: Basics Previous: Items   Contents   Index