Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the Swift Set collection

Shulou Source: shulou.com Published: 2022-06-02 17:21:11 10月04日 Update

This article will explain in detail how to use the Swift Set collection. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Create Set collection / / create Setvar set: Set = [1,2,3] var set2 = Set (arrayLiteral: 1,2,3) 2. Get the element / / set get the minimum value set.min () / / get the first element, indefinite set [set.startIndex] set.first// gets the element through the subscript and can only move backward, not forward / / get the second element set.index (after: set.startIndex) / / get several elements after a subscript [set.index (set.startIndex, offsetBy: 2)] 3. Common method / / get the number of elements set.count// judge empty set if set.isEmpty {print ("set is empty")} / / determine whether the collection contains an element if (set.contains (3)) {print ("set contains 3")} / / insert set.insert (0) / / remove set.remove (2) set.removeFirst () / / remove the element at the specified location. Unpack, get the Optional type, if you remove the element that does not exist EXC_BAD_INSTRUCTIONset.remove (at: set.firstIndex (of: 1)!) set.removeAll () var setStr1: Set = ["1", "2", "3", "4"] var setStr2: Set = ["1", "2", "5", "6"] / / Set take intersection setStr1.intersection (setStr2) / / {"2", "1"} / / Set take intersection setStr1.symmetricDifference (setStr2) / / {"4", "5" "3", "6"} / / Set merge setStr1.union (setStr2) / / {"2", "3", "1", "4", "6", "5"} / / Set take relative complement (difference) A.subtract (B), that is, the set of elements that belongs to A but does not belong to B setStr1.subtract (setStr2) / / {"3", "4"} var eqSet1: Set = [1,2,3] var eqSet2: Set = [3,1,2] / / judge that the Set set is equal if eqSet1 = = eqSet2 {print ("the two sets are equal only when all the elements in the set are equal Independent of the order of elements ")} let set3: Set = [0,1] let set4: Set = [0,1,2] / / judgment subset set3.isSubset (of: set4) / / set3 is a subset of set4, trueset3.isStrictSubset (of: set4) / / set3 is a true subset of set4, true// determines that set4.isSuperset (of: set3) / / set4 is a superset of set3, trueset4.isStrictSuperset (of: set3) / / set4 is a true superset of set3. Set traversal / / traversal element for ele in set4 {print (ele)} / / Enum of the traversal collection for ele in set4.enumerated () {print (index)} / / subscript traverses for index in set4.indices {print (set 4 [index])} / / sort from smallest to largest, and then traverse for ele in set4.sorted (by:

Tags: Elements subsets articles supersets intersections more order good practical minimum from small to large two number from small location content subtraction common article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology MySQL Microsoft Shulou Information