zoukankan      html  css  js  c++  java
  • 不排序和可以重复Key的SortedList。

    同事想实现一个保留添加顺序的字典表,用Hashtableb不能满足要求,普通的SortedList会自动排序,看了一下帮助,实现方法非常简单,代码如下:

    using System;
    using System.Collections;

    namespace testSortedList
    {
        
    class Class1
        {
            [STAThread]
            
    static void Main(string[] args)
            {
                SortedList sl 
    = new SortedList(new MySort());        //不排序
                sl.Add(333,333);
                sl.Add(
    111,111);
                sl.Add(
    222,222);
                sl.Add(
    111,112);

                PrintList(sl);

                Console.ReadLine();
            }

            
    private static void PrintList(SortedList sl)
            {
                
    for(int i=0;i<sl.Count ;i++)
                {
                    Console.WriteLine(
    "{0}\t{1}",sl.GetKey(i),sl.GetByIndex(i));
                }
    //end for
            }//end fn()

        }
        
    public class MySort:IComparer
        {
            
    #region IComparer 成员
            
    public int Compare(object x, object y)
            {
                
    return -1;

                
    //排序
    //            int iResult = (int)x - (int)y;
    //            if(iResult == 0) iResult = -1;
    //            return iResult;

            }
            
    #endregion
        }

    }

    只要Compare函数的返回结果不等于0就可以添加相同的Key,这样可以实现既可以排序,又可以有相同的Key值,可能在某些情况下会用得到。
  • 相关阅读:
    8.16集训
    8.15集训
    Educational Codeforces Round 97 (Rated for Div. 2)
    Codeforces Round #679 (Div. 2, based on Technocup 2021 Elimination Round 1)
    尺取法
    Codeforces Round #677 (Div. 3)
    单调队列
    dfs序
    离散化
    [kuangbin带你飞]专题七 线段树
  • 原文地址:https://www.cnblogs.com/Pharaoh/p/156202.html
Copyright © 2011-2022 走看看