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
| Name | Description |
|---|---|
= | 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
| Name | Description |
|---|---|
| clear | Clears all elements from the container. |
| contains | Checks if the map contains an element with the specified key. |
| count | Returns the number of elements matching the specified key in the container. |
| empty | Checks if the container is empty. |
| erase | Removes elements matching the specified key from the container. |
| extend | Inserts all elements from another container into this container. |
| get | Accesses an element in the map by key. Return default if the key is not found. |
| insert | Inserts a key-value pair into the container. |
| items | Returns a list of elements where each is a pair representing a key-value entry in the map. |
| keys | Returns a list containing all keys present in the map. |
| size | Returns the number of elements in the container. |
| values | Returns a list containing all values present in the map. |