//插入一个值到set中去
var setc:Set = ["Apple","Orange","WaterMelon","Lemon"]
setc.insert("Grape")
print(setc.insert("Pear"))
print(setc)
//在set中删除一个值
var setd:Set = [1,2,3,4,5,6]
//remove one of set
let removedset = setd.remove(3)
print(removedset!)
print(setd)
//remove the fist value of set
let removefirst = setd.removeFirst()
print(removefirst)
print(setd)
//remove all values
setd.removeAll()
print(setd)
//set中的排序问题
let sete:Set = ["red","blue","white","purple","black","yellow"]
print(sete.sorted())
//集合的运算 交集/合集/并集/子集/
//交集 a ^ b
let setm:Set = ["a","c","g","y"]
let setn:Set = ["x","y","z"]
let setmn = setm.intersection(setn)
print(setmn)
//合集 a + b
let setmwithn = setm.union(setn)
print(setmwithn)
//子集 a - b
let setmnsub = setmwithn.subtracting(setn)
print(setmnsub)
result:
(inserted: true, memberAfterInsert: "Pear")
["Pear", "Lemon", "Grape", "Orange", "Apple", "WaterMelon"]
3
[5, 6, 2, 4, 1]
5
[6, 2, 4, 1]
[]
["black", "blue", "purple", "red", "white", "yellow"]
["y"]
["a", "g", "x", "y", "c", "z"]
["a", "g", "c"]
Program ended with exit code: 0