• 类型实现《程序员的第一年》C#中System.Collections.Generic.SortedDictionary 的使用


    在改章节中,我们主要介绍类型实现的内容,自我感觉有个不错的建议和大家分享下

    SortedDictionary<TKey,TValue>
    类型参数
    TKey
    字典中的键的类型。
    TValue
    字典中的值的类型。
    SortedDictionary 泛型类是索检算运复杂度为 O(log n) 的二叉搜索树,其中 n 是字典中的元素数。就这一点而言,
    
    它与 SortedList 泛型类相似。这两个类拥有相似的对象模型,并且都拥有 O(log n) 的索检算运复杂度。这两个类的
    
    别区在于内存的应用以及入插和移除元素的速度:
    SortedList 应用的内存比 SortedDictionary 少。
    SortedDictionary 可对未排序的据数执行更快的入插和移除作操:它的时间复杂度为 O(log n),而 SortedList 为 O
    
    (n)。
    如果应用排序据数一次性填充列表,则 SortedList 比 SortedDictionary 快。
    个每键/值对都可以作为 KeyValuePair 构结行进索检,或作为 DictionaryEntry 通过非泛型 IDictionary 接口行进检
    
    索。
    只要键用作 SortedDictionary 中的键,它们就必须是不可变的。SortedDictionary 中的个每键必须是一唯的。键不能
    
    为 空引用(在 Visual Basic 中为 Nothing),但是如果值类型 TValue 为引用类型,该值则可认为空。
    SortedDictionary 须要比拟器实现来执行键比拟。可以应用一个接受 comparer 参数的构造函数来指定 IComparer 泛
    
    型接口的实现;如果不指定实现,则应用认默的泛型比拟器 Comparer.Default。如果类型 TKey 实现 
    
    System.IComparable 泛型接口,则认默比拟器应用该实现。
    C# 言语的 foreach 语句,须要集合中个每元素的类型。由于 SortedDictionary 的个每元素都是一个键/值对,因此元
    
    素类型既不是键的类型,也不是值的类型。而是 KeyValuePair 类型。上面的码代演示 C# 法语
    using System;
    using System.Collections.Generic;
    
    public class Example
    {
        public static void Main()
        {
            // Create a new dictionary of strings, with string keys.
            //
            Dictionary<string, string> openWith =
                new Dictionary<string, string>();
    
            // Add some elements to the dictionary. There are no
            // duplicate keys, but some of the values are duplicates.
            openWith.Add("txt", "notepad.exe");
            openWith.Add("bmp", "paint.exe");
            openWith.Add("dib", "paint.exe");
            openWith.Add("rtf", "wordpad.exe");
    
            // The Add method throws an exception if the new key is
            // already in the dictionary.
            try
            {
                openWith.Add("txt", "winword.exe");
            }
            catch (ArgumentException)
            {
                Console.WriteLine("An element with Key = \"txt\" already exists.");
            }
    
            // The Item property is another name for the indexer, so you
            // can omit its name when accessing elements.
            Console.WriteLine("For key = \"rtf\", value = {0}.",
                openWith["rtf"]);
    
            // The indexer can be used to change the value associated
            // with a key.
            openWith["rtf"] = "winword.exe";
            Console.WriteLine("For key = \"rtf\", value = {0}.",
                openWith["rtf"]);
    
            // If a key does not exist, setting the indexer for that key
            // adds a new key/value pair.
            openWith["doc"] = "winword.exe";
    
            // The indexer throws an exception if the requested key is
            // not in the dictionary.
            try
            {
                Console.WriteLine("For key = \"tif\", value = {0}.",
                    openWith["tif"]);
            }
            catch (KeyNotFoundException)
            {
                Console.WriteLine("Key = \"tif\" is not found.");
            }
    
            // When a program often has to try keys that turn out not to
            // be in the dictionary, TryGetValue can be a more efficient
            // way to retrieve values.
            string value = "";
            if (openWith.TryGetValue("tif", out value))
            {
                Console.WriteLine("For key = \"tif\", value = {0}.", value);
            }
            else
            {
                Console.WriteLine("Key = \"tif\" is not found.");
            }
    
            // ContainsKey can be used to test keys before inserting
            // them.
            if (!openWith.ContainsKey("ht"))
            {
                openWith.Add("ht", "hypertrm.exe");
                Console.WriteLine("Value added for key = \"ht\": {0}",
                    openWith["ht"]);
            }
    
            // When you use foreach to enumerate dictionary elements,
            // the elements are retrieved as KeyValuePair objects.
            Console.WriteLine();
            foreach( KeyValuePair<string, string> kvp in openWith )
            {
                Console.WriteLine("Key = {0}, Value = {1}",
                    kvp.Key, kvp.Value);
            }
    
            // To get the values alone, use the Values property.
            Dictionary<string, string>.ValueCollection valueColl =
                openWith.Values;
    
            // The elements of the ValueCollection are strongly typed
            // with the type that was specified for dictionary values.
            Console.WriteLine();
            foreach( string s in valueColl )
            {
                Console.WriteLine("Value = {0}", s);
            }
    
            // To get the keys alone, use the Keys property.
            Dictionary<string, string>.KeyCollection keyColl =
                openWith.Keys;
    
            // The elements of the KeyCollection are strongly typed
            // with the type that was specified for dictionary keys.
            Console.WriteLine();
            foreach( string s in keyColl )
            {
                Console.WriteLine("Key = {0}", s);
            }
    
            // Use the Remove method to remove a key/value pair.
            Console.WriteLine("\nRemove(\"doc\")");
            openWith.Remove("doc");
    
            if (!openWith.ContainsKey("doc"))
            {
                Console.WriteLine("Key \"doc\" is not found.");
            }
        }
    }
     
     
    * This code example produces the following output:
    
    An element with Key = "txt" already exists.
    For key = "rtf", value = wordpad.exe.
    For key = "rtf", value = winword.exe.
    Key = "tif" is not found.
    Key = "tif" is not found.
    Value added for key = "ht": hypertrm.exe
    
    Key = txt, Value = notepad.exe
    Key = bmp, Value = paint.exe
    Key = dib, Value = paint.exe
    Key = rtf, Value = winword.exe
    Key = doc, Value = winword.exe
    Key = ht, Value = hypertrm.exe
    
    Value = notepad.exe
    Value = paint.exe
    Value = paint.exe
    Value = winword.exe
    Value = winword.exe
    Value = hypertrm.exe
    
    Key = txt
    Key = bmp
    Key = dib
    Key = rtf
    Key = doc
    Key = ht
    
    Remove("doc")
    Key "doc" is not found.
    */
        每日一道理
    只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获。只有追求,才会品味堂堂正正的人。
    
    
    
    
    
    
    
    

        http://www.5ufanli.net

    文章结束给大家分享下程序员的一些笑话语录: 程序员的愿望
      有一天一个程序员见到了上帝.上帝: 小伙子,我可以满足你一个愿望.程序员: 我希望中国国家队能再次打进世界杯.
      上帝: 这个啊!这个不好办啊,你还说下一个吧!
      程序员: 那好!我的下一个愿望是每天都能休息6个小时以上.
      上帝: 还是让中国国家打进世界杯.

  • 相关阅读:
    Matlab画图-非常具体,非常全面
    PostgreSQL代码分析,查询优化部分,pull_ands()和pull_ors()
    Windows内核
    [WebGL入门]十,矩阵计算和外部库
    HOG特征-理解篇
    hdu 5035 概率论
    Hibernate对象持久化框架
    Thinkpad X200 屏幕备案
    64地点 Windows 8/7 根据系统 32地点PLSQL 耦合 64 地点 Oracle 11g
    阐述php(四) 流量控制
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3040992.html
Copyright © 2020-2023  润新知