MapsThe data type map can be used to store objects of an arbitrary type I together with an associated key of type pointer, item, or int.Remark: Maps are implemented by hashing with chaining. You can think of them as a variant of Dictionary Arrays optimized for keys of type pointer, item, or int. ExampleThe following program generates a list of segments (type item) and associates a random bit (color) with each segment using a map. Then it outputs each segment and its color.#include <LEDA/geo/segment.h> #include <LEDA/core/map.h> #include <LEDA/core/list.h> using namespace leda; int main() { list<segment> seglist; int i; for (i=0;i<10;i++) { segment s(0,i,2*i,3*i); seglist.append(s); } map<segment,int> color; segment s; forall(s,seglist) color[s]=rand_int(0,1); forall(s,seglist) std::cout << s << " " << color[s] << std::endl; return 0; } Strengths
Disadvantages
TipsIf you want to store objects with an associated key of type pointer, item, or int use map, otherwise consider using one of the related data types. |
See also:Data types with key of linearly ordered type: Data types with key of hashed type: Hashing Arrays. Manual Entries: |