
The SortedSet class in C# represents a collection of objects that is maintained in sorted order.
Following are the properties of the SortedSet class −
1Comparer
Gets the IComparer object that is used to order the values in the SortedSet.
2Count
Gets the number of elements in the SortedSet.
3Max
Gets the maximum value in the SortedSet, asdefined by the comparer.
4Min
Gets the minimum value in the SortedSet, as由比较器定义。
以下是SortedSet类的一些方法:
1Add(T)
将元素添加到集合中,并返回一个值,该值表示是否成功添加了元素。
indicates if it was successfully added.
2Clear()
Removes all elements from the set.
3Contains(T)
Determines whether the set contains a specific element.
4CopyTo(T[])
Copies the complete SortedSet to a compatible onedimensional array, starting at the beginning of thetarget array.
5CopyTo(T[], Int32)
Copies the complete SortedSet to a compatible onedimensional array, starting at the specified array index.
6CopyTo(T[], Int32, Int32)
Copies a specified number of elements从SortedSet转换为兼容的一维数组array, starting at the specified array index.
7CreateSetComparer()
Returns an IEqualityComparer object that can be used to创建一个包含个别集合的集合。
示例
现在让我们看一些示例 −
要检查 SortedSet 是否包含特定元素,代码如下 −
实时演示
using System;using System.Collections.Generic;public class Demo { public static void Main() { SortedSet set1 = new SortedSet(); 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 set2 = new SortedSet(); 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)); }}
Output
This will produce the following output −
Elements in SortedSet1...CDDoes the SortedSet1 contains the element DE? = FalseElements in SortedSet2...ABBCCDDEEFHIJKSortedSet2 is a superset of SortedSet1? = True
要获得一个遍历SortedSet的枚举器,代码如下 −
示例
在线演示
using System;using System.Collections.Generic;public class Demo { public static void Main(){ SortedSet set1 = new SortedSet(); 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); } SortedSet set2 = new SortedSet(); 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.Enumerator demoEnum = set2.GetEnumerator(); while (demoEnum.MoveNext()) { string res = demoEnum.Current; Console.WriteLine(res); } }}
Output
This will produce the following output −
Elements in SortedSet1...ABBCCDEFElements in SortedSet2 (Enumerator for SortedSet)...ABBCCDDEEFHIJK
以上就是C# 中的 SortedSet 类的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1434983.html
微信扫一扫
支付宝扫一扫