Set
A container that stores unique elements in no particular order.
Set is an associative container that contains a collection of unique elements. No two elements in a set can have the same value. Sets provide fast insertion, removal, and lookup operations, typically with average constant time complexity. Unlike maps, sets store only the values themselves, not key-value pairs. They are ideal for membership testing, removing duplicates from collections, and mathematical set operations. At the moment, the only types you can store in a Set are: bool, integer, real and string.
Example
var unique_numbers = Set()
unique_numbers.insert(1)
unique_numbers.insert(2)
unique_numbers.insert(1) // Duplicate, won't be added again
print("Set size: ${unique_numbers.size()}") // Will be 2
print("Contains 1: ${unique_numbers.count(1)}") // Will be 1 (true)
print("Contains 3: ${unique_numbers.count(3)}") // Will be 0 (false)
// Remove duplicates from a list
var numbers = [1, 2, 2, 3, 3, 3, 4]
var unique_set = Set(numbers)
Constructors
Set() -> Set
Creates a default-constructed instance of Set.
Set(const Set other) -> Set
Creates a copy of an existing Set object.
Parameters
- other: The Set object to copy.
Symbols
| Name | Description |
|---|---|
= | Assigns the value of the right-hand side to the left-hand side. |
Members
| Name | Description |
|---|---|
| capacity | Returns the number of elements that the container has currently allocated space for. |
| clear | Clears all elements from the container. |
| contains | Checks if the set contains the specified value. |
| count | Returns the number of occurrences of the specified value in the container. |
| empty | Checks if the container is empty. |
| erase | Removes the first occurrence of the specified value from the container. |
| extend | Merge an unordered set into an unordered set. |
| insert | Inserts a value into the unordered set. |
| max | Returns the maximum element in the container. Throws an exception if the container is empty. |
| min | Returns the minimum element in the container. Throws an exception if the container is empty. |
| reserve | Reserves storage for at least the specified number of elements. |
| size | Returns the number of elements in the container. |