首頁  >  文章  >  後端開發  >  C# 排序集

C# 排序集

WBOY
WBOY原創
2024-09-03 15:24:30555瀏覽

按排序順序排列的物件集合在 C# 中稱為 SortedSet,該集合是通用類型,位於命名空間 Systems.Collections.Generic 命名空間下。 SortedSet 類別還提供了數學中的集合操作,例如差、交和並,並且SortedSet 的大小隨著新元素向集合中的添加而增長,因此它是動態集合,可以查看、刪除元素或添加到SortedSet 中的集合中。 SortedSet 按降序排序,SortedSet 中沒有冗餘的元素,這表示只有唯一的元素儲存在 SortedSet 中。

文法:

SortedSet<Type>set_name = new SortedSet<Type>();

其中 Type 是排序集的類型,

set_name 是集合的名稱。

C# 中 SortedSet 的函數

  • SortedSet 用於儲存、檢視和刪除按降序排序的不同元素。
  • SortedSet 中儲存的元素類型必須相同。
  • IReadOnlyCollection 介面、IDeserializationCallBack 介面、IEnumerable 介面、ISet 介面、ISerialized 介面均由 SortedSet 類別實作。
  • SortedSet 可以容納的元素數量稱為 SortedSet 的容量。
  • SortedSet 中不能有重複元素,SortedSet 避免了冗餘。

C# 中 SortedSet 的建構子

以下是 C# SortedSet 的建構子:

1。 SortedSet(): Sorted 類別的新實例已初始化。

2。 SortedSet(IComparer):使用指定為參數的比較器初始化 Sorted 類別的新實例。

3。 SortedSet(IEnumerable):初始化 Sorted 類別的新實例,其中包含從指定為參數的可列舉集合中取得的元素。

4。 SortedSet(IEnumerable, IComparer):初始化 Sorted 類別的新實例,其中包含從指定為參數的可枚舉集合中取得的元素,並使用指定為參數的比較器。

5。 SortedSet(SerializationInfo, StreamingContext):初始化 Sorted 類別的新實例,其中包含序列化的資料。

C# 中 SortedSet 的方法

以下是C# SortedSet的方法:

1。 Add(T):使用 Add(T) 方法將元素加到 SortedSet,成功將元素加入 SortedSet 後,會傳回一個值,表示新增成功。

2。 UnionWith(IEnumerable): 目前已排序物件的變更方式使其包含目前物件中存在的或指定為參數的集合中存在的所有元素。

3。 Clear(): SortedSet 的所有元素都已刪除。

4。 TryGetValue(T,T): 在 SortedSet 中搜尋指定為參數的值,如果找到該值,則傳回相等的值。

5。包含(T):在排序集中檢查指定為參數的元素,以找出它是否存在於排序集中。

6。 ToString(): 傳回代表目前物件的字串。

7。 CopyTo(): 將排序集中的部分元素或排序集中的所有元素複製到一個一維數組,該數組與排序集相容,索引為數組的開頭複製開始的位置或指定的索引。

8。 SymmetricExceptWith(IEnumerable): 目前 Sorted 物件已變更為僅包含目前物件中存在的元素或指定為參數的集合中存在的元素,但不包含兩者中的元素。

9。 CreateSetComparer():使用 CreateSetComparer() 方法傳回 IEqualityComparer 對象,使用此方法建立包含各個集合的集合。

10。 SetEquals(IEnumerable): SetEquals(IEnumerable) 方法檢查排序集的目前物件和參數指定的集合中是否存在相同的元素。

11。 CreateSetComparer(IEqualityComparer):使用 CreateSetComparer(IEqualityComparer) 方法根據指定為參數的比較器傳回 IEqualityComparer 對象,使用此方法建立包含各個集合的集合。

12。 Reverse():使用 Reverse() 方法傳回 IEnumerable,該方法以相反的順序迴圈遍歷排序集。

13。 Equals(Object): 檢查指定為參數的物件是否等於目前物件。

14. RemoveWhere(Predicate): All the elements of the sorted set matching the conditions set by the predicate specified as a parameter is removed.

15. ExceptWith(IEnumerable): The elements in the collection specified as the parameter are removed from the current sorted set object.

16. Remove(T): The item specified as the parameter will be removed from the sorted set.

17. GetEnumerator(): An Enumerator is returned using GetEnumertor() method which loops through the sorted set.

18. Overlaps(IEnumerable): The Overlaps(IEnumerable) method is used to check if the elements in the current sorted set and the elements in the collection specified as parameters are the same.

19. GetHashCode(): The GetHashCode() method is the hash function by default.

20. OnDeserilaization(Object): The event of deserialization is raised after the completion of deserialization and the ISerializable interface is implemented.

21. GetObjectData(SerilaizationInfo, StreamingContext): The data that is necessary to serialize a sorted set object is returned, and the ISerializableinterface is implemented.

22. MemberwiseClone(): The shallow copy of the current object is created.

23. GetType(): The type of the current instance is returned.

24. IsSupersetOf(IEnumerable): The IsSupersetOf(IEnumerable) method is used to determine if the object of a sorted set is a superset of the collection specified as a parameter.

25. GetViewBetween(T,T): A view of the subset in the sorted set is returned.

26. IsSubsetOf(IEnumerable): The IsSubsetOf(IEnumerable) method is used to determine if the object of a sorted set is a subset of the collection specified as a parameter.

27. IntersectWith(IEnumerable)The current Sorted object is changed in such a way that it consists only of the elements present in the collection specified as a parameter.

28. IsProperSupersetOf(IEnumerable): The IsProperSupersetOf(IEnumerable) method is used to determine if the object of the sorted set is a proper superset of the collection specified as a parameter.

29. IsProperSubsetOf(IEnumerable): The IsProperSubsetOf(IEnumerable) method is used to determine if the object of a sorted set is a proper subset of the collection specified as a parameter.

Example

Below are the examples of C# SortedSet:

C# program to create a SortedSetby using Add(T) method and then demonstrate the Remove(T) method and IsSubsetof(IEnumerable) method.

Code:

using System;
using System.Collections.Generic;
class program
{
public static void Main()
{
SortedSet<string>Set = new SortedSet<string>();
Set.Add("Shobha");
Set.Add("Ramya");
Set.Add("Nandan");
Set.Add("Nalina");
Set.Add("Sindhu");
Console.WriteLine("The elements of the sorted set are:");
foreach(string t in Set)
{
Console.WriteLine(t);
}
Console.WriteLine("The elements of the sorted set after using Remove method are:");
Set.Remove("Sindhu");
Set.Remove("Nalina");
foreach(string x in Set)
{
Console.WriteLine(x);
}
SortedSet<string> Set1 = new SortedSet<string>();
Set1.Add("Sahana");
Set1.Add("Suhaas");
Set1.Add("Nalina");
Console.WriteLine("Checking if the elements of the sorted set is a subset of the first set:");
Console.WriteLine(Set1.IsSubsetOf(Set));
}
}

Output:

C# 排序集

Explanation: In the above program, a class called program is called. Then the main method is called. Then a sorted set to store the strings is created. Then elements are added to the sorted set using add() method. Then the foreach loop is used to display the elements of the sorted set. Then remove() method is used to remove the elements of the sorted set. Then again the foreach loop is used to display the elements of the sorted set. Then again a new sorted set to store strings is created. Then again elements are added to the new sorted set using add() method. Then IsSubsetof() method is used to check if the newly created sorted set is a subset of the first sorted set. The output of the program is shown in the snapshot above.

以上是C# 排序集的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn