• [学习官方例子]TCustomComparer


    代码如下:

    program TCustomComparerTest;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils, Generics.Defaults, Generics.Collections;
    
    type
      TCustomerStringComparer = class(TCustomComparer<String>)
      public
        function Compare(const Left, Right: String): Integer; override;
        function Equals(const Left, Right: String): Boolean; override;
        function GethashCode(const Value: String): Integer; override;
      end;
      { TCustomerStringComparer }
    
    function TCustomerStringComparer.Compare(const Left, Right: String): Integer;
    begin
      Result := CompareText(Left, Right);
    end;
    
    function TCustomerStringComparer.Equals(const Left, Right: String): Boolean;
    begin
      Result := CompareText(Left, Right)=0;
    end;
    
    function TCustomerStringComparer.GethashCode(const Value: String): Integer;
    begin
      Result := Length(Value);
    end;
    
    procedure btSortClick();
    var
      List: TList<string>;
      i: Integer;
    begin
      List := TList<string>.Create(TCustomerStringComparer.Create());
    
      for i := 0 to 5 do
        List.Add('第'+IntToStr(8-i)+'个数');
    
      List.Sort;
      for i := 0 to 5 do
        writeln(List[i]);
      List.Free;
    end;
    
    procedure btCountClick();
    var
      Dictionary: TDictionary<String, Cardinal>;
      i: Integer;
      AString: string;
    begin
      Dictionary := TDictionary<String, Cardinal>.Create(TCustomerStringComparer.Create());
    
      for i := 0 to 5 do
      begin
        AString := '第'+IntToStr(i)+'个数';
        if Dictionary.ContainsKey(AString) then
          Dictionary[AString] := Dictionary[AString] + 1
        else
          Dictionary.Add(AString,  1);
      end;
    
      for AString in Dictionary.Keys do
        writeln(AString+':'+UIntToStr(Dictionary[AString]));
    
      Dictionary.Free;
    
    
    
    end;
    
    begin
      writeln('btSortClick:');
      btSortClick;
      writeln(#10,'btSortClick:');
      btCountClick;
    
      readln;
    end.

    效果图:

    mx3485D

  • 相关阅读:
    卡尔曼滤波器的简单推导
    data_quick 进度
    面试
    MapServer+TileCache+Apache+Python24 构建KS数据服务器
    PYTHONPATH 可以跨版本 方便使用 (本文为windows方法)转~
    20130923
    20130909
    error C2504 类的多层继承 头文件包含
    int long 等基础类型在不同平台的大小
    mysql python image 图像存储读取
  • 原文地址:https://www.cnblogs.com/wouldguan/p/2211082.html
Copyright © 2020-2023  润新知