加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi 中的哈希表(2): TStringHash

发布时间:2020-12-15 09:59:31 所属栏目:大数据 来源:网络整理
导读:unit Unit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender
unit Unit1;

interface

uses
  Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  IniFiles;  //TStringHash 来自 IniFiles 单元
var
  Hash: TStringHash;

{ TStringHash 的功能非常简单,如果需要更多功能应该使用: THashedStringList
  TStringHash 与 THashedStringList、TStringList 最大的不同是:
  TStringHash 的 Key 必须是 String; Value 必须是 Integer.
  如果这不适合你的要求,建一个 TMyHash 也不是难事 }


//建立哈希表
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Hash := TStringHash.Create(26);  //26 是表的初始大小,可以省略使用默认值256
  for i := 65 to 90 do
  begin
    Hash.Add(Chr(i),i);  //如果表不够大,会自动增加的
  end;
end;

//读取
procedure TForm1.Button1Click(Sender: TObject);
var
  num: Integer;
begin
  num := Hash.ValueOf('Z');
  ShowMessage(IntToStr(num));  //90
end;

//修改、删除、清空
procedure TForm1.Button2Click(Sender: TObject);
begin
  Hash.Modify('Z',100);  //修改
  Hash.Remove('A');      //删除
  Hash.Clear;            //清空

  {没了,就这些功能}
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Hash.Free;
end;

end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读