基础概念
sortedset 是 c# 中的一个集合类型,位于 system.collections.generic 命名空间下。它是一个自动排序的集合,用于存储不重复的元素,并且会根据元素的自然顺序(默认排序)或自定义比较器进行排序,内部使用红黑树数据结构来维护元素的有序性。
- 自动排序:每次添加或删除元素时,sortedset 都会自动调整以保持元素的排序状态。
- 不重复元素:sortedset 不允许重复的元素。如果尝试添加一个已经存在的元素,该操作会被忽略。
- 高效性:sortedset 内部使用红黑树(一种自平衡二叉搜索树)实现,因此查找、插入和删除操作的时间复杂度为 o(log n)。
主要特性
- 自动保持元素排序:元素会根据其自然顺序(需实现 icomparable<t> 接口)或自定义比较器(icomparer<t>)排序。
- 不包含重复元素:尝试添加已有元素时,add 方法返回 false,集合保持不变。
- 支持集合操作:提供并集、交集、差集等操作。
- 支持子集视图:可以通过方法获取某个范围内的元素。
- 快速访问边界值:提供 min 和 max 属性,快速获取最小和最大元素。
创建和初始化
基本创建方式
使用默认比较器(升序)
// 使用默认比较器(升序) sortedset<int> numbers = new sortedset<int>();
使用自定义比较器
// 使用自定义比较器 sortedset<string> names = new sortedset<string>(stringcomparer.ordinalignorecase);
从现有集合创建
// 从现有集合创建 int[] array = { 5, 2, 8, 1, 9 }; sortedset<int> sortednumbers = new sortedset<int>(array); // 结果:{1, 2, 5, 8, 9}
使用集合初始化器
// 使用集合初始化器 sortedset<string> fruits = new sortedset<string> { "apple", "banana", "cherry" };
自定义比较器
降序排列
// 降序排列 sortedset<int> descendingnumbers = new sortedset<int>(comparer<int>.create((x, y) => y.compareto(x)));
自定义对象排序
// 自定义对象排序 public class person : icomparable<person> { public string name { get; set; } public int age { get; set; } public int compareto(person other) { if (other == null) return 1; return this.age.compareto(other.age); // 按年龄排序 } } sortedset<person> people = new sortedset<person>();
使用自定义比较器
// 或使用自定义比较器 sortedset<person> peoplebyname = new sortedset<person>( comparer<person>.create((p1, p2) => string.compare(p1.name, p2.name)) );
基本操作
添加和删除元素
sortedset<int> numbers = new sortedset<int>();
添加元素
// 添加元素 bool added1 = numbers.add(5); // true,成功添加 bool added2 = numbers.add(3); // true,成功添加 bool added3 = numbers.add(5); // false,元素已存在 console.writeline(string.join(", ", numbers)); // 输出:3, 5
删除元素
// 删除元素 bool removed = numbers.remove(3); // true,成功删除 numbers.remove(10);
清空集合
// 清空集合 numbers.clear();
查询操作
sortedset<int> numbers = new sortedset<int> { 1, 3, 5, 7, 9 };
检查元素是否存在
// 检查元素是否存在 bool contains = numbers.contains(5); // true
获取元素数量
// 获取元素数量 int count = numbers.count; // 5
检查是否为空
// 检查是否为空 bool isempty = numbers.count == 0; // false
获取最小值和最大值
// 获取最小值和最大值 int min = numbers.min; // 1 int max = numbers.max; // 9
范围查询
使用 getviewbetween 方法获取指定范围内的元素子集
sortedset<int> numbers = new sortedset<int> { 1, 3, 5, 7, 9, 11, 13 }; // 获取视图(不创建新集合) sortedset<int> subset1 = numbers.getviewbetween(3, 9); // 结果:{3, 5, 7, 9} sortedset<int> subset2 = numbers.getviewbetween(4, 10); // 结果:{5, 7, 9} // 视图会反映原集合的变化 numbers.add(6); console.writeline(string.join(", ", subset2)); // 输出:5, 6, 7, 9
集合运算
并集、交集、差集
sortedset<int> set1 = new sortedset<int> { 1, 2, 3, 4, 5 }; sortedset<int> set2 = new sortedset<int> { 4, 5, 6, 7, 8 };
并集:unionwith 将另一个集合的元素合并到 sortedset 中。
// 并集(修改 set1) set1.unionwith(set2); console.writeline(string.join(", ", set1)); // 1, 2, 3, 4, 5, 6, 7, 8
交集:intersectwith 保留与另一个集合的交集。
// 重新初始化 set1 = new sortedset<int> { 1, 2, 3, 4, 5 }; // 交集(修改 set1) set1.intersectwith(set2); console.writeline(string.join(", ", set1)); // 4, 5
差集:exceptwith 删除与另一个集合相交的元素。
// 重新初始化 set1 = new sortedset<int> { 1, 2, 3, 4, 5 }; // 差集(set1 中有但 set2 中没有的元素) set1.exceptwith(set2); console.writeline(string.join(", ", set1)); // 1, 2, 3
对称差集:symmetricexceptwith 两个集合中不共同拥有的元素
// 对称差集(两个集合中不共同拥有的元素) set1 = new sortedset<int> { 1, 2, 3, 4, 5 }; set1.symmetricexceptwith(set2); console.writeline(string.join(", ", set1)); // 1, 2, 3, 6, 7, 8
集合关系判断
sortedset<int> set1 = new sortedset<int> { 1, 2, 3 }; sortedset<int> set2 = new sortedset<int> { 1, 2, 3, 4, 5 }; sortedset<int> set3 = new sortedset<int> { 2, 3 }; sortedset<int> set4 = new sortedset<int> { 6, 7 };
子集判断
// 子集判断 bool issubset = set1.issubsetof(set2); // true bool ispropersubset = set1.ispropersubsetof(set2); // true bool issuperset = set2.issupersetof(set1); // true bool ispropersuperset = set2.ispropersupersetof(set1); // true
重叠判断
// 重叠判断 bool overlaps = set1.overlaps(set3); // true(有共同元素2,3) bool overlaps2 = set1.overlaps(set4); // false(无共同元素)
相等判断
// 相等判断 bool areequal = set1.setequals(set3); // false
遍历和枚举
基本遍历
sortedset<string> fruits = new sortedset<string> { "banana", "apple", "cherry" };
foreach 遍历(按排序顺序)
// foreach 遍历(按排序顺序) foreach (string fruit in fruits) { console.writeline(fruit); // apple, banana, cherry }
使用枚举器
// 使用枚举器 using (var enumerator = fruits.getenumerator()) { while (enumerator.movenext()) { console.writeline(enumerator.current); } }
反向遍历
sortedset<int> numbers = new sortedset<int> { 1, 3, 5, 7, 9 }; // 反向遍历 foreach (int number in numbers.reverse()) { console.writeline(number); // 9, 7, 5, 3, 1 }
sortedset 的优点和适用场景
优点
- 自动保持元素排序,无需手动干预。
- 确保元素唯一性,避免重复。
- 高效的操作性能(o(log n))。
- 支持集合操作和子集视图。
适用场景
- 需要有序且不重复的元素集合,例如排行榜、时间线。
- 实现优先级队列(尽管 c# 有 priorityqueue<t>)。
- 执行集合操作,如并集、交集等。
sortedset 与其他集合类型的区别
- 与 hashset<t> 的区别:
- hashset<t> 不保持顺序,查找时间为 o(1)。
- sortedset<t> 保持顺序,查找时间为 o(log n)。
- 与 list<t> 的区别:
- list<t> 允许重复元素,不自动排序。
- sortedset<t> 不允许重复,自动排序。
- 与 sortedlist<tkey, tvalue> 的区别:
- sortedlist<tkey, tvalue> 是键值对集合,键排序。
- sortedset<t> 是元素集合,元素本身排序。
到此这篇关于c#中sortedset的具体使用的文章就介绍到这了,更多相关c# sortedset内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论