搜尋
首頁後端開發C#.Net教程C# 中的 SortedSet 類
C# 中的 SortedSet 類Aug 26, 2023 am 09:29 AM

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 SortedSet.Gets the maximum value in the SortedSet, as defined by the comparer.Gets the minimum value in the SortedSet< ;T>, as 由比較器定義。
##Sr.No
Comparer Gets the IComparer object that is used to order the values in the SortedSet.

##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 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。如有侵權,請聯絡admin@php.cn刪除
char數組在C語言中如何使用char數組在C語言中如何使用Apr 03, 2025 pm 03:24 PM

char 數組在 C 語言中存儲字符序列,聲明為 char array_name[size]。訪問元素通過下標運算符,元素以空終止符 '\0' 結尾,用於表示字符串終點。 C 語言提供多種字符串操作函數,如 strlen()、strcpy()、strcat() 和 strcmp()。

char在C語言中如何處理特殊字符char在C語言中如何處理特殊字符Apr 03, 2025 pm 03:18 PM

C語言中通過轉義序列處理特殊字符,如:\n表示換行符。 \t表示製表符。使用轉義序列或字符常量表示特殊字符,如char c = '\n'。注意,反斜杠需要轉義兩次。不同平台和編譯器可能有不同的轉義序列,請查閱文檔。

char在C語言字符串中的作用是什麼char在C語言字符串中的作用是什麼Apr 03, 2025 pm 03:15 PM

在 C 語言中,char 類型在字符串中用於:1. 存儲單個字符;2. 使用數組表示字符串並以 null 終止符結束;3. 通過字符串操作函數進行操作;4. 從鍵盤讀取或輸出字符串。

C語言各種符號的使用方法C語言各種符號的使用方法Apr 03, 2025 pm 04:48 PM

C 語言中符號的使用方法涵蓋算術、賦值、條件、邏輯、位運算符等。算術運算符用於基本數學運算,賦值運算符用於賦值和加減乘除賦值,條件運算符用於根據條件執行不同操作,邏輯運算符用於邏輯操作,位運算符用於位級操作,特殊常量用於表示空指針、文件結束標記和非數字值。

c#多線程和異步的區別c#多線程和異步的區別Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

char在C語言中如何進行類型轉換char在C語言中如何進行類型轉換Apr 03, 2025 pm 03:21 PM

在 C 語言中,char 類型轉換可以通過:強制類型轉換:使用強制類型轉換符將一種類型的數據直接轉換為另一種類型。自動類型轉換:當一種類型的數據可以容納另一種類型的值時,編譯器自動進行轉換。

C語言 sum 的作用是什麼?C語言 sum 的作用是什麼?Apr 03, 2025 pm 02:21 PM

C語言中沒有內置求和函數,需自行編寫。可通過遍歷數組並累加元素實現求和:循環版本:使用for循環和數組長度計算求和。指針版本:使用指針指向數組元素,通過自增指針遍歷高效求和。動態分配數組版本:動態分配數組並自行管理內存,確保釋放已分配內存以防止內存洩漏。

避免 C語言 switch 語句中 default 引起的錯誤避免 C語言 switch 語句中 default 引起的錯誤Apr 03, 2025 pm 03:45 PM

避免 C 語言 switch 語句中 default 引發的錯誤的策略:使用枚舉代替常量,限制 case 語句的值為枚舉的有效成員。在最後一個 case 語句中使用 fallthrough,讓程序繼續執行以下代碼。對於沒有 fallthrough 的 switch 語句,始終添加一個 default 語句進行錯誤處理或提供默認行為。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具