typedef Edge Key; typedef double Value;
To make easy to use them - especially as template parameters - there are map concepts like by graph classes.
operator
[]. value_typed_variable = map_instance[key_value];
set()
member function. map_instance.set(key_value, value_typed_expression);
operator
[] to providing you constant or non-constant reference to the value belonging to a key, so you have a direct access to the memory address where it is stored. ListGraph::EdgeMap<int> weight(graph);
If the graph class is extendable or erasable the map will automatically follow the changes you make. If a new node is added a default value is mapped to it. You can define the default value by passing a second argument to the map's constructor.
ListGraph::EdgeMap<int> weight(graph, 13);
VALUE
has to have copy constructor.
Of course VALUE
can be a rather complex type.
For practice let's see the following template function (from maps-summary.cc in the demo directory)!
template < typename GRAPH, typename MAP > typename MAP::Value summary( GRAPH& gr, MAP& m ) { typename MAP::Value summ = typename MAP::Value(); for( typename GRAPH::NodeIt n(gr); n != lemon::INVALID; ++n ) summ += m[n]; return summ; }
And the usage is simpler than the declaration suggests. The compiler deduces the template specialization, so the usage is like a simple function call.
std::cout << "The summary of assigned values is " << summary(gr,value) << std::endl;
Most of the time you will probably use graph maps, but keep in mind, that in LEMON maps are more general and can be used widely.
If you want some 'real-life' examples see the next page, where we discuss Algorithms (coming soon) and will use maps hardly. Or if you want to know more about maps read these advanced map techniques.