C# 中的 SortedSet 类

王林
发布: 2023-08-26 09:29:17
转载
1047 人浏览过

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
1 Comparer

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

2 Count

Gets the number of elements in the SortedSet.

3 Max

Gets the maximum value in the SortedSet, as defined by the comparer.

4 Min

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

以下是SortedSet类的一些方法:

序号 方法与描述
1 Add(T)

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

indicates if it was successfully added.

2 Clear()

Removes all elements from the set.

3 Contains(T)

Determines whether the set contains a specific element.

4 CopyTo(T[])

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

5 CopyTo(T[], Int32)

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

6 CopyTo(T[], Int32, Int32)

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

7 CreateSetComparer()

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

示例

现在让我们看一些示例 −

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

 实时演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<string> set1 = new SortedSet<string>();
      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<string> set2 = new SortedSet<string>();
      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...
CD
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True
登录后复制

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

示例

 在线演示

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      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<string> set2 = new SortedSet<string>();
      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<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}
登录后复制

Output

This will produce the following output −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK
登录后复制

以上是C# 中的 SortedSet 类的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!