Lớp SortedSet trong C # đại diện cho một tập hợp các đối tượng được duy trì theo thứ tự đã sắp xếp.
Sau đây là các thuộc tính của lớp SortedSet -
| Sr.No | Thuộc tính &Mô tả |
|---|---|
| 1 | So sánh Lấy đối tượng IComparer |
| 2 | Đếm Nhận số phần tử trong SortedSet |
| 3 | Tối đa Nhận giá trị lớn nhất trong SortedSet |
| 4 | Tối thiểu Nhận giá trị nhỏ nhất trong SortedSet |
Sau đây là một số phương thức của lớp SortedSet -
| Sr.No | Phương pháp &Mô tả |
|---|---|
| 1 | Thêm (T) Thêm một phần tử vào tập hợp và trả về giá trị thatindicates nếu nó được thêm thành công. |
| 2 | Clear () Loại bỏ tất cả các phần tử khỏi tập hợp. |
| 3 | Chứa (T) Xác định xem tập hợp có chứa một phần tử cụ thể hay không. |
| 4 | CopyTo (T []) Sao chép hoàn chỉnh SortedSet |
| 5 | CopyTo (T [], Int32) Sao chép SortedSet |
| 6 | CopyTo (T [], Int32, Int32) Sao chép một số phần tử cụ thể từ SortedSet |
| 7 | CreateSetComparer () Trả về một đối tượng IEqualityComparer có thể được sử dụng để tạo một bộ sưu tập có chứa các bộ riêng lẻ. |
Ví dụ
Bây giờ chúng ta hãy xem một số ví dụ -
Để kiểm tra xem SortedSet có chứa một phần tử cụ thể hay không, mã như sau -
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));
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
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
Để có được một điều tra viên lặp qua SortedSet, mã như sau -
Ví dụ
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);
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Elements in SortedSet1... AB BC CD EF Elements in SortedSet2 (Enumerator for SortedSet)... AB BC CD DE EF HI JK