In Swift 1.2 there is a Set object, which you can use to create a static typed Set.
I cannot find out how to get the object at a certain index. It has a subscript
that allows you to do the following: mySet[setIndex]
.
This retrieves the object at that setIndex
. But now I want to get an object from a certain Int
index.
var myObject = mySet[sIndex];
But how do I create a SetIndex with a certain 'index'?
There is no index attached to any element in a python set. So they do not support any indexing or slicing operation.
As sets are unordered, we can not subscript them with indexes. This does not work like lists.
A set in c++ is an associative(STL) container used to store unique elements that are stored in a specific sorted order(increasing or decreasing). Elements of the set are unique, i.e., no duplicate values can be stored in the set because each value in the set is a key, and the set doesn't support indexing.
You can offsetBy:
from .startIndex
:
let mySet: Set = ["a", "b", "c", "d"] mySet[mySet.index(mySet.startIndex, offsetBy: 2)] // -> something from the set.
You can advancedBy()
from .startIndex
:
let mySet: Set = ["a", "b", "c", "d"] mySet[mySet.startIndex.advancedBy(2)] // -> something from the set.
Similar to String
, you have to advance()
from .startIndex
:
let mySet: Set = ["a", "b", "c", "d"] mySet[advance(mySet.startIndex, 2)] // -> something from the set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With