当前位置: 首页>>代码示例>>C#>>正文


C# SortedSet<T>类代码示例

本文整理汇总了C#中System.Collections.Generic.SortedSet<T>的典型用法代码示例。如果您正苦于以下问题:C# SortedSet<T>类的具体用法?C# SortedSet<T>怎么用?C# SortedSet<T>使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SortedSet<T>类属于System.Collections.Generic命名空间,在下文中一共展示了SortedSet<T>类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Get a list of the files to use for the sorted set.
            IEnumerable<string> files1 =
                Directory.EnumerateFiles(@"\\archives\2007\media",
                "*", SearchOption.AllDirectories);

            // Create a sorted set using the ByFileExtension comparer.
            var mediaFiles1 = new SortedSet<string>(new ByFileExtension());

            // Note that there is a SortedSet constructor that takes an IEnumerable,
            // but to remove the path information they must be added individually.
            foreach (string f in files1)
            {
                mediaFiles1.Add(f.Substring(f.LastIndexOf(@"\") + 1));
            }

            // Remove elements that have non-media extensions.
            // See the 'IsDoc' method.
            Console.WriteLine("Remove docs from the set...");
            Console.WriteLine($"\tCount before: {mediaFiles1.Count}");
            mediaFiles1.RemoveWhere(IsDoc);
            Console.WriteLine($"\tCount after: {mediaFiles1.Count}");

            Console.WriteLine();

            // List all the avi files.
            SortedSet<string> aviFiles = mediaFiles1.GetViewBetween("avi", "avj");

            Console.WriteLine("AVI files:");
            foreach (string avi in aviFiles)
            {
                Console.WriteLine($"\t{avi}");
            }

            Console.WriteLine();

            // Create another sorted set.
            IEnumerable<string> files2 =
                Directory.EnumerateFiles(@"\\archives\2008\media",
                    "*", SearchOption.AllDirectories);

            var mediaFiles2 = new SortedSet<string>(new ByFileExtension());

            foreach (string f in files2)
            {
                mediaFiles2.Add(f.Substring(f.LastIndexOf(@"\") + 1));
            }

            // Remove elements in mediaFiles1 that are also in mediaFiles2.
            Console.WriteLine("Remove duplicates (of mediaFiles2) from the set...");
            Console.WriteLine($"\tCount before: {mediaFiles1.Count}");
            mediaFiles1.ExceptWith(mediaFiles2);
            Console.WriteLine($"\tCount after: {mediaFiles1.Count}");

            Console.WriteLine();

            Console.WriteLine("List of mediaFiles1:");
            foreach (string f in mediaFiles1)
            {
                Console.WriteLine($"\t{f}");
            }

            // Create a set of the sets.
            IEqualityComparer<SortedSet<string>> comparer =
                SortedSet<string>.CreateSetComparer();

            var allMedia = new HashSet<SortedSet<string>>(comparer);
            allMedia.Add(mediaFiles1);
            allMedia.Add(mediaFiles2);
        }
        catch(IOException ioEx)
        {
            Console.WriteLine(ioEx.Message);
        }

        catch (UnauthorizedAccessException AuthEx)
        {
            Console.WriteLine(AuthEx.Message);
        }
    }

    // Defines a predicate delegate to use
    // for the SortedSet.RemoveWhere method.
    private static bool IsDoc(string s)
    {
        s = s.ToLower();
        return (s.EndsWith(".txt") ||
            s.EndsWith(".xls") ||
            s.EndsWith(".xlsx") ||
            s.EndsWith(".pdf") ||
            s.EndsWith(".doc") ||
            s.EndsWith(".docx"));
    }
}

// Defines a comparer to create a sorted set
// that is sorted by the file extensions.
public class ByFileExtension : IComparer<string>
{
    string xExt, yExt;

    CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer();

    public int Compare(string x, string y)
    {
        // Parse the extension from the file name. 
        xExt = x.Substring(x.LastIndexOf(".") + 1);
        yExt = y.Substring(y.LastIndexOf(".") + 1);

        // Compare the file extensions. 
        int vExt = caseiComp.Compare(xExt, yExt);
        if (vExt != 0)
        {
            return vExt;
        }
        else
        {
            // The extension is the same, 
            // so compare the filenames. 
            return caseiComp.Compare(x, y);
        }
    }
}
开发者ID:.NET开发者,项目名称:System.Collections.Generic,代码行数:134,代码来源:SortedSet


注:本文中的System.Collections.Generic.SortedSet<T>类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。