在C#中的SortedSet中获取最小值
要获得SortedSet中的最小值,代码如下-
示例
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); } Console.WriteLine("Maximum element in SortedSet1 = " + set1.Max); Console.WriteLine("Minimum element in SortedSet1 = " + set1.Min); 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); } Console.WriteLine("Maximum element in SortedSet2 = " + set2.Max); Console.WriteLine("Minimum element in SortedSet2 = " + set2.Min); } }
输出结果
这将产生以下输出-
Elements in SortedSet1... AB BC CD EF Maximum element in SortedSet1 = EF Minimum element in SortedSet1 = AB Elements in SortedSet2 (Enumerator for SortedSet)... AB BC CD DE EF HI JK Maximum element in SortedSet2 = JK
示例
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { public static void Main(){ SortedSet<int> set1 = new SortedSet<int>(); set1.Add(5); set1.Add(50); set1.Add(20); set1.Add(10); set1.Add(70); set1.Add(60); set1.Add(55); set1.Add(95); Console.WriteLine("Elements in SortedSet..."); foreach (int res in set1){ Console.WriteLine(res); } Console.WriteLine("Maximum element in SortedSet = " + set1.Max); Console.WriteLine("Minimum element in SortedSet = " + set1.Min); } }
输出结果
这将产生以下输出-
Elements in SortedSet... 5 10 20 50 55 60 70 95 Maximum element in SortedSet = 95 Minimum element in SortedSet = 5