SimpleCxxLib
packageclass HashMap<KeyType,ValueType>
Map
class
except for the fact that it uses a hash table as its underlying
representation. Although the HashMap
class operates in
constant time, the iterator for HashMap
returns the
values in a seemingly random order.
Constructor | |
Initializes a new empty map that associates keys and values of the specified types. | |
Methods | |
Removes all entries from this map. | |
Returns true if there is an entry for key in this map. | |
Returns the value associated with key in this map. | |
Returns true if this map contains no entries. | |
Iterates through the map entries and calls fn(key, value) for each one. | |
Associates key with value in this map. | |
Removes any entry for key from this map. | |
Returns the number of entries in this map. | |
Converts the map to a printable string representation. | |
Operator | |
Selects the value associated with key . |
HashMap();
==
operator, and there must be a free function
with the following signature:
int hashCode(KeyType key);that returns a positive integer determined by the key. This interface exports
hashCode
functions for string
and
the C++ primitive types.
Usage:
HashMap<KeyType,ValueType> map; HashMap<KeyType,ValueType> map = {{ k1, v1}, { k2, v2 }};
int size() const;
Usage:
int nEntries = map.size();
bool isEmpty() const;
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) const;
key
in this map.
If key
is not found, get
returns the
default value for ValueType
.
Usage:
ValueType value = map.get(key);
bool containsKey(KeyType key) const;
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();
string toString();
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;
fn(key, value)
for each one. The keys are processed in an undetermined order.
Usage:
map.mapAll(fn);
ValueType & operator[](KeyType key); ValueType operator[](KeyType key) const;
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]