#include "map.h"

class Map<KeyTypeValueType>

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.

The map uses a binary search tree (BST) structure internally. Because of this choice of internal representation, the KeyType for the keys stored in a Map must define a natural ordering through a less function and/or < operator so that the keys can be compared and ordered. The ValueType does not need to provide any such natural ordering.
Constructor
Map()  O(1) Initializes a new empty map that associates keys and values of the specified types.
Methods
clear()  O(N) Removes all entries from this map.
containsKey(key)  O(log N) Returns true if there is an entry for key in this map.
equals(map)  O(N) Returns true if the two maps contain the same elements.
get(key)  O(log N) Returns the value associated with key in this map.
isEmpty()  O(1) Returns true if this map contains no entries.
keys()  O(N) Returns a Vector copy of all keys in this map.
mapAll(fn)  O(N) Iterates through the map entries and calls fn(key, value) for each one.
put(keyvalue)  O(log N) Associates key with value in this map.
remove(key)  O(log N) Removes any entry for key from this map.
size()  O(1) Returns the number of entries in this map.
toString()  O(N) Converts the map to a printable string representation.
values()  O(N) Returns a Vector copy of all values in this map.
Operators
map[key]  O(log N) Selects the value associated with key.
map1 == map1  O(N) Returns true if map1 and map2 contain the same elements.
map1 != map2  O(N) Returns true if map1 and map2 are different.
ostream << map O(N) Outputs the contents of the map to the given output stream.
istream >> map O(N log N) Reads the contents of the given input stream into the map.

Constructor detail


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

Usage:

Map<KeyType, ValueType> map;

Method detail


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

Usage:

map.clear();

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

Usage:

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

bool equals(const Map& map) const;
Returns true if the two maps contain exactly the same set of key/value pairs. Identical in behavior to the == operator.

Usage:

if (map.equals(map2)) ...

ValueType get(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 isEmpty() const;
Returns true if this map contains no entries.

Usage:

if (map.isEmpty()) ...

Vector<KeyType> keys() const;
Returns a Vector copy of all keys in this map. The elements will appear in the same order that a for-each loop over the map would produce them. Because a map cannot contain duplicate keys, the elements of the vector will be unique.

Usage:

Vector<KeyType> keys = map.keys();

Available since: 2014/02/01 version of C++ library


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);

void put(KeyType key, 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);

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

Usage:

map.remove(key);

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

Usage:

int nEntries = map.size();

string toString() const;
Converts the map to a printable string representation, such as "{k1:v1, k2:v2, k3:v3}". The key/value pairs will be listed in ascending order by key.

Usage:

string str = map.toString();

Vector<ValueType> values() const;
Returns a Vector copy of all values in this map. The elements will appear in the same order that a for-each loop over the map would produce them. A map can contain duplicate values, so the elements of the vector are not guaranteed to be unique.

Usage:

Vector<ValueType> values = map.values();

Available since: 2014/02/01 version of C++ library


Operator detail


ValueType& operator[](KeyType key);
ValueType operator[](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]