在 C# 中检查 SortedSet 是否包含特定元素
要检查 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
广告