C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# SortedSet

C#中SortedSet的具体使用

作者:枫景Maple

SortedSet是 .NET Framework 4.0引入的一个泛型集合类,它实现了一个自动排序的集合,内部使用红黑树数据结构来维护元素的有序性,下面就来介绍一下如何使用

基础概念

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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文