class Map<KeyType, ValueType>
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 | ||
O(1) | Initializes a new empty map that associates keys and values of the specified types. | |
Methods | ||
O(N) | Removes all entries from this map. | |
O(log N) | Returns true if there is an entry for key in this map. |
|
O(N) | Returns true if the two maps contain the same elements. |
|
O(log N) | Returns the value associated with key in this map. |
|
O(1) | Returns true if this map contains no entries. |
|
O(N) | Returns a Vector copy of all keys in this map. |
|
O(N) | Iterates through the map entries and calls fn(key, value) for each one. |
|
O(log N) | Associates key with value in this map. |
|
O(log N) | Removes any entry for key from this map. |
|
O(1) | Returns the number of entries in this map. | |
O(N) | Converts the map to a printable string representation. | |
O(N) | Returns a Vector copy of all values in this map. |
|
Operators | ||
O(log N) | Selects the value associated with key . |
|
O(N) | Returns true if map1 and map2 contain the same elements. |
|
O(N) | Returns true if map1 and map2 are different. |
|
O(N) | Outputs the contents of the map to the given output stream. | |
O(N log N) | Reads the contents of the given input stream into the map. |
Map();
Usage:
Map<KeyType, ValueType> map;
void clear();
Usage:
map.clear();
bool containsKey(KeyType key) const;
true
if there is an entry for key
in this map.
Usage:
if (map.containsKey(key)) ...
bool equals(const Map& map) const;
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;
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;
true
if this map contains no entries.
Usage:
if (map.isEmpty()) ...
Vector<KeyType> keys() const;
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;
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);
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);
key
from this map.
Usage:
map.remove(key);
int size() const;
Usage:
int nEntries = map.size();
string toString() const;
"{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;
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
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]