The SimpleCxxLib package


#include "map.h"

class Map<KeyType,ValueType>

This 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() Initializes a new empty map that associates keys and values of the specified types.
Methods
clear() Removes all entries from this map.
containsKey(key) Returns true if there is an entry for key in this map.
get(key) Returns the value associated with key in this map.
isEmpty() Returns true if this map contains no entries.
mapAll(fn) Iterates through the map entries and calls fn(key, value) for each one.
put(key, value) Associates key with value in this map.
remove(key) Removes any entry for key from this map.
size() Returns the number of entries in this map.
toString() Converts the map to a printable string representation.
Operator
map[key] Selects the value associated with key.

Constructor detail


Map();
Initializes a new empty map that associates keys and values of the specified types.

Usage:

Map<KeyType,ValueType> map;
Map<KeyType,ValueType> map = {{ k1, v1}, { k2, v2 }};

Method detail


int size() const;
Returns the number of entries in this map.

Usage:

int nEntries = map.size();

bool isEmpty() const;
Returns true if this map contains no entries.

Usage:

if (map.isEmpty()) ...

void put(const KeyType & key, const ValueType & value);
Associates 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(const KeyType & key) const;
Returns the value associated with key in this map. If key is not found, get returns the default value for ValueType.

Usage:

ValueType value = map.get(key);

bool containsKey(const KeyType & key) const;
Returns true if there is an entry for key in this map.

Usage:

if (map.containsKey(key)) ...

void remove(const KeyType & key);
Removes any entry for key from this map.

Usage:

map.remove(key);

void clear();
Removes all entries from this map.

Usage:

map.clear();

string toString();
Converts the map to a printable string representation.

Usage:

string str = map.toString();

void mapAll(void (*fn)(KeyType, ValueType)) const;
void mapAll(void (*fn)(const KeyType &, const ValueType &)) const;
void mapAll(FunctorType fn) const;
Iterates through the map entries and calls fn(key, value) for each one. The keys are processed in ascending order, as defined by the comparison function.

Usage:

map.mapAll(fn);

Operator detail


ValueType & operator[](const KeyType & key);
ValueType operator[](const KeyType & key) const;
Selects the value associated with 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]