同桌上课用手指进去了好爽_欧美丰满熟妇xxⅹⅹ性大i_成人av天天日天天拍拍_猛男gay帅男gay男男同志_欧美va天堂在线观看_人妻无码av中文系列三里桃花_亚欧免费无码在线观看_久久久精品国产亚洲av水_日韩在线免费看污污污_2021无码专区人妻系列日韩

首頁 優(yōu)化推廣 C#中SortedDictionary 的使用

C#中SortedDictionary 的使用

來源: | 時間:2013/6/11 0:55:13 |
SortedDictionary<TKey,TValue>
類型參數(shù)

TKey

字典中的鍵的類型。

TValue

字典中的值的類型。

SortedDictionary 泛型類是檢索運(yùn)算復(fù)雜度為 O(log n) 的二叉搜索樹,其中 n 是字典中的元素數(shù)。就這一點(diǎn)而言,它與 SortedList 泛型類相似。這兩個類具有相似的對象模型,并且都具有 O(log n) 的檢索運(yùn)算復(fù)雜度。這兩個類的區(qū)別在于內(nèi)存的使用以及插入和移除元素的速度:

  • SortedList 使用的內(nèi)存比 SortedDictionary 少。

  • SortedDictionary 可對未排序的數(shù)據(jù)執(zhí)行更快的插入和移除操作:它的時間復(fù)雜度為 O(log n),而 SortedList 為 O(n)。

  • 如果使用排序數(shù)據(jù)一次性填充列表,則 SortedListSortedDictionary 快。

每個鍵/值對都可以作為 KeyValuePair 結(jié)構(gòu)進(jìn)行檢索,或作為 DictionaryEntry 通過非泛型 IDictionary 接口進(jìn)行檢索。

只要鍵用作 SortedDictionary 中的鍵,它們就必須是不可變的。SortedDictionary 中的每個鍵必須是唯一的。鍵不能為 空引用(在 Visual Basic 中為 Nothing),但是如果值類型 TValue 為引用類型,該值則可以為空。

SortedDictionary 需要比較器實(shí)現(xiàn)來執(zhí)行鍵比較??梢允褂靡粋€接受 comparer 參數(shù)的構(gòu)造函數(shù)來指定 IComparer 泛型接口的實(shí)現(xiàn);如果不指定實(shí)現(xiàn),則使用默認(rèn)的泛型比較器 Comparer.Default。如果類型 TKey 實(shí)現(xiàn) System.IComparable 泛型接口,則默認(rèn)比較器使用該實(shí)現(xiàn)。

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";
 &

服務(wù)熱線

153 8323 9821

功能和特性

價格和優(yōu)惠

網(wǎng)站和維護(hù)

推廣和優(yōu)化

微信服務(wù)號