Set特点:
1、无序
2、唯一性,具有去重功能
3、高效地完成集合的一些基本操作:求交集、并集等
定义Set-指明类型
var skillsOfA:Set = ["吃饭","睡觉","追剧"]
var skillsOfB:Set<String> = ["吃饭","睡觉","追剧"]
var vowel = Set(["a","e","i","o","u"])
基本操作
print(skillsOfA.count)
print(skillsOfA.isEmpty)
print(vowel.first!)//取出的不是第一个?
//遍历
for i in vowel{
print(i)
}
//Set比较
let setA:Set = [1,2,3]
let setB:Set = [3,2,1]
let setC:Set = [3,2,1,3,2]
print(setA == setB)//true
print(setA == setC)//true
print(setC.count)
增删改查
//增:考虑到集合是无序存放数据,不用关心数据是添加在末尾还是其他。
skillsOfA.insert("swift")
skillsOfA.insert("html")
//删
skillsOfA.remove("swift")
print(skillsOfA)
//修改 把html-html5 先删除再添加
//查找
print(skillsOfA.contains("iOS"))
集合操作:交集、并集、子集、父集等
var a:Set = [1, 3, 5, 7, 9]
var b:Set = [0, 2, 4, 6, 8]
var c:Set = [2, 3, 5, 7]
print(a.union(b)) //a并b,不会影响a的值
print(a)
a.formUnion(b) //a并b,影响a的值
print(a)
print(a.intersection(c))//a交b,不会影响a的值
a.formIntersection(c)// a交b值赋给a,影响a的值
print(a)
a=Set([1, 3, 5, 7, 9])
c=Set([1, 3, 5,7,9])
print(c.isSubset(of:a))//判断c是否为a的子集-true
print(c.isStrictSubset(of:a) )//判断c是否为a的真子集-false
print(a.isSuperset(of:c))////判断a是否为c的父集-true
print(a.isStrictSuperset(of:c))////判断a是否为c的真父集false