要检查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)); } }
输出结果
这将产生以下输出-
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
让我们来看另一个例子-
Let us see another example: using System; using System.Collections.Generic; public class Demo { public static void Main(){ SortedSet<int> mySet = new SortedSet<int>(); mySet.Add(100); mySet.Add(200); mySet.Add(300); mySet.Add(400); Console.WriteLine("Elements in SortedSet..."); foreach (int res in mySet){ Console.WriteLine(res); } Console.WriteLine("Does the SortedSet contains the element 400? = "+mySet.Contains(400)); } }
输出结果
这将产生以下输出-
Elements in SortedSet... 100 200 300 400 Does the SortedSet contains the element 400? = True