首頁 >後端開發 >C#.Net教程 >C# 中的 SortedSet 類

C# 中的 SortedSet 類

王林
王林轉載
2023-08-26 09:29:171102瀏覽

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 −

##Property & Description1221;T>.Gets the number of elements in the SortedSet8742468051c85b06f0a0af9e3e506b5c.Gets the maximum value in the SortedSet8742468051c85b06f0a0af9e3e506b5c, as defined by the comparer.Gets the minimum value in the SortedSet< ;T>, as 由比較器定義。
##Sr.No
Comparer Gets the IComparer8742468051c85b06f0a0af9e3e506b5c object that is used to order the values in the SortedSet8742468051c85b06f0a0af9e3e506b5c.

##2221;

##Count

3

Max

4

Min

以下是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 SortedSet8742468051c85b06f0a0af9e3e506b5c to a compatible onedimensional array, starting at the beginning of the target array. 5

#CopyTo(T[], Int32)

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

##CopyTo(T[ ], Int32, Int32)

Copies a specified number of elements 從SortedSet8742468051c85b06f0a0af9e3e506b5c轉換為相容的一維數組 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刪除