Giới thiệu
Phương thức IsProperSubsetOf() của SortedSet<T> kiểm tra xem tập hợp hiện tại có phải là tập con thực sự (proper subset) của tập hợp chỉ định hay không. Điều này nghĩa là tất cả phần tử của tập hợp hiện tại đều nằm trong tập hợp chỉ định và tập hợp chỉ định có ít nhất một phần tử không nằm trong tập hợp hiện tại.
Ví dụ 1
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet set1 = new SortedSet();
set1.Add(20);
set1.Add(40);
set1.Add(60);
set1.Add(80);
set1.Add(100);
set1.Add(120);
set1.Add(140);
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (int res in set1)
{
Console.WriteLine(res);
}
SortedSet set2 = new SortedSet();
set2.Add(20);
set2.Add(40);
set2.Add(60);
Console.WriteLine("Các phần tử trong SortedSet2...");
foreach (int res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 là tập con thực sự của SortedSet1? = " + set2.IsProperSubsetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... 20 40 60 80 100 120 140 Các phần tử trong SortedSet2... 20 40 60 SortedSet2 là tập con thực sự của SortedSet1? = True
Ví dụ 2
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet set1 = new SortedSet();
set1.Add(20);
set1.Add(40);
set1.Add(60);
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (int res in set1)
{
Console.WriteLine(res);
}
SortedSet set2 = new SortedSet();
set2.Add(20);
set2.Add(40);
set2.Add(60);
Console.WriteLine("Các phần tử trong SortedSet2...");
foreach (int res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 là tập con thực sự của SortedSet1? = " + set2.IsProperSubsetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... 20 40 60 Các phần tử trong SortedSet2... 20 40 60 SortedSet2 là tập con thực sự của SortedSet1? = False
Lưu ý quan trọng
- Tập con thực sự (Proper Subset): A ⊂ B ⇔ Tất cả phần tử của A nằm trong B VÀ B có ít nhất một phần tử không trong A.
- Nếu hai tập hợp bằng nhau,
IsProperSubsetOf()trả vềfalse. - Phương thức
IsSubsetOf()(không có "Proper") trả vềtruenếu tất cả phần tử của A nằm trong B (cho phép A = B). - Độ phức tạp: O(n) với n là số phần tử của tập hợp hiện tại.