class Map<KeyType,ValueType>Map class maintains an association between
keys and values. The types used for keys and values are
specified using templates, which makes it possible to use
this structure with any data type.
| Constructor | |
| Map() Map(cmpFn) | Initializes a new empty map that associates keys and values of the specified types. |
| Methods | |
| size() | Returns the number of entries in this map. |
| isEmpty() | Returns true if this map contains no entries. |
| put(key, value) | Associates key with value in this map. |
| get(key) | Returns the value associated with key in this map. |
| containsKey(key) | Returns true if there is an entry for key in this map. |
| remove(key) | Removes any entry for key from this map. |
| clear() | Removes all entries from this map. |
| mapAll(fn) mapAll(fn, data) | Iterates through the keys in this map and calls fn(key) for each one. |
| Operator | |
| map[key] | Selects the value associated with key. |
| Macro | |
| foreach(KeyType key in map) | Iterates over the keys in the map. |
Map(int (*cmpFn)(KeyType, KeyType) = operatorCmp);
operatorCmp function from cmpfn.h,
which applies the built-in operators < and
== to determine the ordering.
Usage:
Map<KeyType,ValueType> map; Map<KeyType,ValueType> map(cmpFn);
int size();
Usage:
int nEntries = map.size();
bool isEmpty();
true if this map contains no entries.
Usage:
if (map.isEmpty()) . . .
void put(KeyType key, ValueType value);
key with value in this map.
Any previous value associated with key is replaced
by the new value.
Usage:
map.put(key, value);
ValueType get(KeyType key);
key in this map.
If key is not found, the get method
signals an error.
Usage:
ValueType value = map.get(key);
bool containsKey(KeyType key);
true if there is an entry for key
in this map.
Usage:
if (map.containsKey(key)) . . .
void remove(KeyType key);
key from this map.
Usage:
map.remove(key);
void clear();
Usage:
map.clear();
void mapAll(void (*fn)(KeyType key)); void mapAll(void (*fn)(KeyType, ClientDataType &), ClientDataType & data);
fn(key)
for each one. The keys are processed in ascending order, as defined
by the comparison function. The second form of the call allows the
client to pass a data value of any type to the callback function.
Usage:
map.mapAll(fn); map.mapAll(fn, data);
ValueType & operator[](KeyType key);
key. This syntax
makes it easy to think of a map as an "associative array"
indexed by the key type. If key is already present
in the map, this function returns a reference to its associated
value. If key is not present in the map, a new entry is created
whose value is set to the default for the value type.
Usage:
map[key]
foreach (KeyType key in map) . . .