当前位置: 首页 > news >正文

Swift学习笔记第三节:Set类型

1、代码

import Foundationvar set1: Set<Int> = [1, 2, 3, 4, 3]
print("定义1: \(set1)")
var set2 = Set(1...4)
print("定义2: \(set2)")
print("长度: \(set2.count)")
print("是否为空: \(set2.isEmpty)")
set1.insert(99)
set1.update(with: 33)
print("插入: \(set1)")
let (isInsert,val) = set1.insert(3)
print("是否插入成功: \(isInsert), 插入的值: \(val)")
set1.remove(4)
print("删除: \(set1)")
set1.removeAll()
print("删除所有: \(set1)")let set3 = Set(1...7)
let set4 = Set(4...9)
let set5 = Set(2...4)
print("交集: \(set3.intersection(set4))")
print("并集: \(set3.union(set4))")
print("差集: \(set3.subtracting(set4))")
print("补集: \(set3.symmetricDifference(set4))")
print("是不是子集: \(set5.isSubset(of: set3))")
print("是不是超集: \(set3.isSuperset(of: set5))")
print("随机数: \(set3.randomElement() ?? -1)")
print("排序: \(set5.sorted())")for item in set5 {print("遍历: \(item)")
}for (index,value) in set5.enumerated() {print("索引: \(index), 值: \(value)")
}set5.forEach{ item inprint("遍历: \(item)")
}

2、运行结果

定义1: [3, 1, 4, 2]
定义2: [4, 2, 1, 3]
长度: 4
是否为空: false
插入: [3, 33, 99, 1, 4, 2]
是否插入成功: false, 插入的值: 3
删除: [3, 33, 99, 1, 2]
删除所有: []
交集: [4, 5, 7, 6]
并集: [6, 3, 2, 4, 5, 1, 7, 8, 9]
差集: [1, 3, 2]
补集: [3, 2, 1, 8, 9]
是不是子集: true
是不是超集: true
随机数: 1
排序: [2, 3, 4]
遍历: 2
遍历: 4
遍历: 3
索引: 0, 值: 2
索引: 1, 值: 4
索引: 2, 值: 3
遍历: 2
遍历: 4
遍历: 3
http://www.lryc.cn/news/267492.html

相关文章:

  • 【前端】安装指定版本的nodejs
  • 多商户小程序开源商城源码 打造微信商城新零售网店
  • 云仓酒庄的品牌雷盛红酒LEESON分享干红是纯葡萄酿造的吗?
  • PHP函数学习总结
  • 5G RedCap:轻量5G技术的新宠
  • 【LeetCode 热题 HOT 100】题解笔记 —— Day04
  • rust中的超时处理
  • DML语言(重点)———update
  • Mybatis使用详解
  • 云原生周刊:Karmada 成为 CNCF 孵化项目 | 2023.12.25
  • 【开源】基于JAVA的学校热点新闻推送系统
  • Java基于TCP网络编程的群聊功能
  • CentOS+ISCSI
  • RHCE9学习指南 第11章 网络配置
  • Qt如何在控制台项目中使用opencv打开视频
  • Node.js 默认包管理器 npm 详解
  • vue利用深拷贝解决修改不能取消的问题
  • MATLAB - 使用 YOLO 和基于 PCA 的目标检测,对 UR5e 的半结构化智能垃圾箱拣选进行 Gazebo 仿真
  • 个性化定制的知识付费小程序,为用户提供个性化的知识服务,知识付费saas租户平台
  • 基于flask和echarts的新冠疫情实时监控系统源码+数据库,后端基于python的flask框架,前端主要是echarts
  • 总结js中遍历对象属性的方法
  • 编写fastapi接口服务
  • RasaGPT对话系统的工作原理
  • C++设计模式 #7 工厂方法(Factory Method)
  • 信息网络协议基础-接入网技术
  • springboot 自动装配原理
  • 前端---表格标签
  • 【软件工程】可执行文件和数据分离
  • Linux dirs命令
  • 有什么好用的C/C++源代码混淆工具?