zoukankan      html  css  js  c++  java
  • Delphi 中的哈希表: THashedStringList

    转自万一博客

    Delphi 中的哈希表: THashedStringList

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses
      IniFiles;  //THashedStringList 来自 IniFiles 单元
    var
      Hash: THashedStringList;
    
    { THashedStringList 继承自 TStringList, 只是覆盖了 IndexOf、IndexOfName 两个方法以增加效率;
      如果注重效率而不需要太多功能, 可以使用 TStringHash, 它是直接从 TObject 继承的数组链表 }
    
    
    //建立哈希表 (也可从文本文件加载)
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i: Integer;
    begin
      Hash := THashedStringList.Create;
    
      for i := 97 to 122 do
      begin
        Hash.Add(Chr(i) + '=' + IntToStr(i));
      end;
    
      ShowMessage(Hash.Text);
    {
    构建结果:
      a=97
      b=98
      c=99
      d=100
      e=101
      f=102
      g=103
      h=104
      i=105
      j=106
      k=107
      l=108
      m=109
      n=110
      o=111
      p=112
      q=113
      r=114
      s=115
      t=116
      u=117
      v=118
      w=119
      x=120
      y=121
      z=122
    }
    end;
    
    //检索哈希表
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i: Integer;
    begin
      i := Hash.IndexOf('z=122');
      ShowMessage(IntToStr(i));  //25
    
      i := Hash.IndexOfName('z');
      ShowMessage(IntToStr(i));  //25
    end;
    
    //基本操作
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      Hash.Values['a'] := '65';       //赋值
      Hash.ValueFromIndex[0] := '65'; //用索引赋值
    
      ShowMessage(Hash.Values['z']);       //122, 通过 name 取值
      ShowMessage(Hash.ValueFromIndex[25]);//122, 用索引取值
    
      {其他操作参加 TStringList}
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      Hash.Free;
    end;
    
    end.
    
  • 相关阅读:
    PHP curl_exec函数
    PHP curl_escape函数
    PHP curl_error函数
    PHP curl_errno函数
    PHP curl_copy_handle函数
    PHP curl_close函数
    PHP 利用 curl 发送 post get del put patch 请求
    PHP cURL 函数
    PHP 实例
    PHP 实例
  • 原文地址:https://www.cnblogs.com/bjxsky/p/4670127.html
Copyright © 2011-2022 走看看