it编程 > 编程语言 > Asp.net

C#中SortedSet的具体使用

33人参与 2025-08-13 Asp.net

基础概念

sortedset 是 c# 中的一个集合类型,位于 system.collections.generic 命名空间下。它是一个自动排序的集合,用于存储不重复的元素,并且会根据元素的自然顺序(默认排序)或自定义比较器进行排序,内部使用红黑树数据结构来维护元素的有序性。

主要特性

创建和初始化

基本创建方式

使用默认比较器(升序)

// 使用默认比较器(升序)
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 的优点和适用场景

优点

适用场景

sortedset 与其他集合类型的区别

到此这篇关于c#中sortedset的具体使用的文章就介绍到这了,更多相关c# sortedset内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

您想发表意见!!点此发布评论

推荐阅读

C# Serilog 日志的使用小结

08-13

使用c#进行串口通信的实现示例

08-13

C# Opacity 不透明度的具体使用

08-13

C#和Unity中的解释器模式使用方式

08-14

C#和Unity中的中介者模式使用方式

08-14

如何选到你的芯动之选?一文读懂英特尔 Intel Core i3、i5、i7和i9处理器

08-14

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论