next up previous contents
Next: Methods; Inlining Up: Some basics of object-oriented Previous: The Standard Template Library   Contents


Associative arrays/maps

In C-arrays, one needs that indices start at zero and are consecutive. In transportation and many other areas, items such as nodes and streets have names or numbers. In our context, the nodes/links have numbers, and they are unique, but not consecutive. What we want is a data structure that deals with this in a straightforward way, i.e. where we can retrieve a node with ID ``231'' by node[231]. Associative arrays do this. They are used as follows:


{}
#include <map>
...
typedef map<int,Node*> Nodes ;
...
Nodes nodes ;
...
// allocate space for new node and fill with information:
Node* node = new Node(id,xCoord,yCoord); 

// register this node with the global nodes array:
nodes[id] = node;   
...

Use of this now is:


{}
cout << "ID:" << nodes[213]->id() << endl ;
cout << "X :" << nodes[213]->x() << endl ;



2004-02-02