C# 中的 SortedSet 类

c# 中的 sortedset 类

The SortedSet class in C# represents a collection of objects that is maintained in sorted order.

Following are the properties of the SortedSet class −

Sr.No Property & Description

1Comparer

Gets the IComparer object that is used to order the values in the SortedSet.

2Count

Gets the number of elements in the SortedSet.

3Max

Gets the maximum value in the SortedSet, asdefined by the comparer.

4Min

Gets the minimum value in the SortedSet, as由比较器定义。

以下是SortedSet类的一些方法:

序号 方法与描述

1Add(T)

将元素添加到集合中,并返回一个值,该值表示是否成功添加了元素。

indicates if it was successfully added.

2Clear()

Removes all elements from the set.

3Contains(T)

Determines whether the set contains a specific element.

4CopyTo(T[])

Copies the complete SortedSet to a compatible onedimensional array, starting at the beginning of thetarget array.

5CopyTo(T[], Int32)

Copies the complete SortedSet to a compatible onedimensional array, starting at the specified array index.

6CopyTo(T[], Int32, Int32)

Copies a specified number of elements从SortedSet转换为兼容的一维数组array, starting at the specified array index.

7CreateSetComparer()

Returns an IEqualityComparer object that can be used to创建一个包含个别集合的集合。

示例

现在让我们看一些示例 −

要检查 SortedSet 是否包含特定元素,代码如下 −

 实时演示

using System;using System.Collections.Generic;public class Demo {   public static void Main() {      SortedSet set1 = new SortedSet();      set1.Add("CD");      set1.Add("CD");      set1.Add("CD");      set1.Add("CD");      Console.WriteLine("Elements in SortedSet1...");      foreach (string res in set1) {         Console.WriteLine(res);      }      Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));      SortedSet set2 = new SortedSet();      set2.Add("BC");      set2.Add("CD");      set2.Add("DE");      set2.Add("EF");      set2.Add("AB");      set2.Add("HI");      set2.Add("JK");      Console.WriteLine("Elements in SortedSet2...");      foreach (string res in set2) {         Console.WriteLine(res);      }      Console.WriteLine("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));   }}

Output

This will produce the following output −

Elements in SortedSet1...CDDoes the SortedSet1 contains the element DE? = FalseElements in SortedSet2...ABBCCDDEEFHIJKSortedSet2 is a superset of SortedSet1? = True

要获得一个遍历SortedSet的枚举器,代码如下 −

示例

 在线演示

using System;using System.Collections.Generic;public class Demo {   public static void Main(){      SortedSet set1 = new SortedSet();      set1.Add("AB");      set1.Add("BC");      set1.Add("CD");      set1.Add("EF");      Console.WriteLine("Elements in SortedSet1...");      foreach (string res in set1) {         Console.WriteLine(res);      }      SortedSet set2 = new SortedSet();      set2.Add("BC");      set2.Add("CD");      set2.Add("DE");      set2.Add("EF");      set2.Add("AB");      set2.Add("HI");      set2.Add("JK");      Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");      SortedSet.Enumerator demoEnum = set2.GetEnumerator();      while (demoEnum.MoveNext()) {         string res = demoEnum.Current;         Console.WriteLine(res);      }   }}

Output

This will produce the following output −

Elements in SortedSet1...ABBCCDEFElements in SortedSet2 (Enumerator for SortedSet)...ABBCCDDEEFHIJK

以上就是C# 中的 SortedSet 类的详细内容,更多请关注创想鸟其它相关文章!

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 10:28:07
下一篇 2025年12月10日 03:29:43

相关推荐

  • C# 程序找出 Sin(x) 的值

    简介 在本文中,我们将了解 C# 程序来找出 Sin(x) 的值。正弦是 Sin(x) 的另一个名称。这是一个三角角公式。角度的正弦是直角梯形中斜边长度与垂线长度的比例。强大的计算机语言 C# 可用于解决具有挑战性的数学问题。找出 sin(x) 的值(其中 x 是任意以弧度表示的角度)就是这些问题之…

    2025年12月17日
    000
  • 如何在 C# 中比较两个元组?

    元组比较是在 C# 7.3 之后出现的。 使用 C# 中的相等运算符轻松比较两个元组。 假设我们有两个元组 – var one = (x: 1, y: 2);var two = (p: 1, 2: 3, r: 3, s:4); 要比较它们,只需使用 == 运算符 – if (…

    2025年12月17日
    000
  • C# 相当于 Java 函数式接口

    Java 的函数式接口在 C# 中相当于 Delegate。 让我们看看 Java 中函数式接口的实现 – 示例 @FunctionalInterfacepublic interface MyInterface { void invoke();}public class Demo { v…

    2025年12月17日
    000
  • C# 中的反射

    反射对象用于在运行时获取类型信息。允许访问正在运行的程序的元数据的类位于 System.Reflection 命名空间中。 以下是 Reflections 的应用 – 它允许在运行时查看属性信息。 它允许检查程序集中的各种类型并实例化这些类型。 它允许后期绑定到方法和属性 它允许在运行时…

    2025年12月17日
    000
  • C# 中的泛型委托是什么?

    使用通用委托,您不需要定义委托语句。它们在系统命名空间中定义。 您可以使用类型参数定义通用委托。例如 – delegate T myDelegete(T n); 示例 以下示例展示了如何在 C# 中创建通用委托 – using System;using System.Coll…

    2025年12月17日
    000
  • C#中有哪些类?

    定义类时,您就定义了数据类型的蓝图。对象是类的实例。构成类的方法和变量称为类的成员。 类定义以关键字 class 开头,后跟类名;以及由一对花括号括起来的类主体。以下是类定义的一般形式 – class class_name { // member variables variable1;…

    2025年12月17日
    000
  • C#程序从绝对路径获取文件名

    介绍 让我们尝试了解C#程序从绝对路径获取文件名。我们将看到 File 类下的 GetFileName 方法的用法,该方法用于获取文件名,另一个方法 GetFileNameWithoutExtension 将返回指定路径字符串的文件名,不带扩展名。要从两个绝对路径获取相对路径,我们将使用 C# 中的…

    2025年12月17日
    000
  • C# 中缺少哪些 C++ 功能?

    C# 是一种简单、现代、通用、面向对象的编程语言,由 Microsoft 在 Anders Hejlsberg 领导的 .NET 计划中开发。 C++ 是由 Bjarne 开发的中级编程语言Stroustrup 于 1979 年开始在贝尔实验室工作。 C++ 可以在多种平台上运行,例如 Window…

    2025年12月17日
    000
  • 如何在 C# 中验证电子邮件地址?

    在 C# 中验证电子邮件地址有多种方法。 System.Net.Mail -System.Net.Mail 命名空间包含用于将电子邮件发送到的类用于传送的简单邮件传输协议 (SMTP) 服务器。 System.Text.RegularExpressions – 表示不可变的正则表达式。 …

    2025年12月17日
    000
  • C# 中的 BigInteger 类

    在 C# 中使用 BigInteger 处理大数字。要为 BigInteger 添加的程序集是 System。数值。 在 C# 中,大整数位于 System.Numerics.BigInteger 中。 语法 BigInteger 的语法 – [SerializableAttribute…

    2025年12月17日
    000
  • | 之间有什么区别?和||或者 C# 中的运算符?

    | 运算符 | 运算符计算其操作数的逻辑或。如果 x 或 y 之一评估为 true,则 x | y 的结果为 true。否则,结果为 false。 即使左操作数评估为 true,| 运算符也会评估两个操作数,以便操作结果为 true,而不管右操作数的值如何。 || 运算符 条件逻辑或运算符 ||,也…

    2025年12月17日
    000
  • 汇编语言和c语言的区别有哪些

    区别:1、汇编语言效率高,C语言效率比较低;2、汇编语言对硬件的可操控性强,C语言硬件可操控性比较差;3、汇编语言目标代码体积小,C语言目标代码体积大;4、汇编语言不易维护,C语言容易维护;5、汇编语言可移植性很差,C语言可移植性很好等。 本教程操作环境:windows7系统、c99版本、Dell …

    2025年12月17日
    000
  • c程序是由什么构成

    c程序是由函数构成,是由一个main函数和若干个其他函数构成的。函数是C程序的基本单位,被调用的函数可以是系统提供的库函数,也可以是用户根据需要自己定义的函数。 本教程操作环境:windows7系统、c99版本、Dell G3电脑。 C源程序是由函数组成的。函数是C程序的基本单位,被调用的函数可以是…

    2025年12月17日
    000
  • c语言怎么进行强制类型转换

    在c语言中,可以通过“(新类型名称) 数据或表达式”语句来进行强制类型转换;例如“(float) 100”就是将数值100(默认为int类型)转换为float类型,“(int)(x+y)”就是将表达式“x+y”的结果转换为int整型。 本教程操作环境:windows7系统、c99版本、Dell G3…

    2025年12月17日
    000
  • c语言输入成绩怎么判断等级

    判断方法:1、用“switch(成绩/10){case 9:A;..case 6:D;default:E;}”语句;2、用“if(成绩>=90)A;else if(成绩>=80)B;..else if(成绩>=60)D;elseE;”语句。 本教程操作环境:windows7系统、c…

    好文分享 2025年12月17日
    000
  • c语言怎么进行字符串比较

    比较方法:1、bcmp(),比较字符串的前n个字节是否相等;2、strcmp(),区分大小写的比较字符串;3、stricmp(),不区分大小写的比较字符串;4、strncmp()或strnicmp(),区分大小写的比较字符串的前n个字符。 本教程操作环境:windows7系统、c99版本、Dell …

    2025年12月17日
    000
  • c语言函数的三种调用方式是什么

    函数的三种调用方式:1、函数作为表达式中的一项出现在表达式中,例“z=max(x,y)”;2、函数作为一个单独的语句,例“printf(“%d”,a)”;3、函数作为调用另一个函数时的实参,例“printf(“%d”,max(x,y))”。 本教程操作…

    2025年12月17日
    000
  • c语言怎么求字符串的长度并输出

    c语言求字符串的长度并输出的方法:首先使用strlen()函数计算出字符串的长度,并赋值给变量len,语法“len=strlen(字符串);”;然后使用printf()函数输出长度即可,语法“printf(“%dn”,len);”。 本教程操作环境:windows7系统、c9…

    2025年12月17日
    000
  • c语言中保留两位小数怎么表示

    在C语言中,表示方法为“%.2lf”,语法格式为“printf(“%.2lf,%.2lfn”,元素)”。“%.2lf”在“printf()”语句里,整数部分全部输出,小数部分输出2位,不足两位的后面补0,大于两位的截短到两位。 本教程操作环境:windows7系统、C++17…

    2025年12月17日 好文分享
    000
  • double的输入格式符是什么

    使用scanf语句时,double的输入格式符是“%lf”,不能使用“%f”;使用printf语句时,可以使用“%f”,printf中没有定义%lf,但是很多系统会接受。因此建议使用double类型时,用“%lf”输入避免出错。 本教程操作环境:windows7系统、C++17版、Dell G3电脑…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信