Swift中的Set的删除添加计算等问题

//插入一个值到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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值