Giới thiệu
Phương thức IsProperSupersetOf() của SortedSet<T> kiểm tra xem tập hợp hiện tại có phải là siêu tập hợp thực sự (proper superset) của tập hợp chỉ định hay không. Điều này nghĩa là tập hợp hiện tại chứa tất cả phần tử của tập hợp chỉ định và có ít nhất một phần tử không nằm trong tập hợp chỉ định.
Ví dụ 1
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet set1 = new SortedSet();
set1.Add(10);
set1.Add(20);
set1.Add(30);
set1.Add(40);
set1.Add(50);
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(10);
set2.Add(20);
set2.Add(30);
set2.Add(40);
set2.Add(50);
set2.Add(60);
set2.Add(70);
set2.Add(80);
set2.Add(90);
set2.Add(100);
Console.WriteLine("Các phần tử trong SortedSet2...");
foreach (int res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 là siêu tập hợp thực sự của SortedSet1? = " + set2.IsProperSupersetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... 10 20 30 40 50 60 Các phần tử trong SortedSet2... 10 20 30 40 50 60 70 80 90 100 SortedSet2 là siêu tập hợp 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(10);
set1.Add(10); // Trùng lặp
set1.Add(10);
set1.Add(10);
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (int res in set1)
{
Console.WriteLine(res);
}
SortedSet set2 = new SortedSet();
set2.Add(10);
set2.Add(20);
set2.Add(30);
set2.Add(40);
set2.Add(50);
set2.Add(60);
set2.Add(70);
set2.Add(80);
set2.Add(90);
set2.Add(100);
Console.WriteLine("Các phần tử trong SortedSet2...");
foreach (int res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("SortedSet2 là siêu tập hợp thực sự của SortedSet1? = " + set2.IsProperSupersetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... 10 Các phần tử trong SortedSet2... 10 20 30 40 50 60 70 80 90 100 SortedSet2 là siêu tập hợp thực sự của SortedSet1? = True
Lưu ý quan trọng
- Siêu tập hợp thực sự (Proper Superset): A ⊃ B ⇔ A chứa tất cả phần tử của B VÀ A có ít nhất một phần tử không trong B.
- Nếu hai tập hợp bằng nhau (cùng phần tử),
IsProperSupersetOf()trả vềfalse. - Phương thức
IsSupersetOf()(không có "Proper") trả vềtruenếu A chứa tất cả phần tử của 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 nhỏ hơn.