Skip to main content

Map

An associative container that stores key-value pairs in sorted order.

Map is an ordered associative container where each element is a key-value pair. Keys are unique and automatically sorted, providing efficient lookup, insertion, and removal operations. Maps are ideal for situations where you need to associate values with unique keys and want to maintain sorted order or need efficient key-based access. The underlying implementation typically uses a balanced tree structure.

Example


var scores = ["Alice": 95, "Bob": 87, "Charlie": 92]
scores.insert(MapPair("Kirk", 32))

print("Alice's score: ${scores["Alice"]}")
print("Map size: ${scores.size()}")

Constructors

Map() -> Map

Creates a default-constructed instance of Map.

Map(const Map other) -> Map

Creates a copy of an existing Map object.

Parameters

  • other: The Map object to copy.

Symbols

NameDescription
=Assigns the value of the right-hand side to the left-hand side.
[]Accesses an element in the map by key. Throws an exception if the key is not found.

Members

NameDescription
clearClears all elements from the container.
containsChecks if the map contains an element with the specified key.
countReturns the number of elements matching the specified key in the container.
emptyChecks if the container is empty.
eraseRemoves elements matching the specified key from the container.
extendInserts all elements from another container into this container.
getAccesses an element in the map by key. Return default if the key is not found.
insertInserts a key-value pair into the container.
itemsReturns a list of elements where each is a pair representing a key-value entry in the map.
keysReturns a list containing all keys present in the map.
sizeReturns the number of elements in the container.
valuesReturns a list containing all values present in the map.