zoukankan      html  css  js  c++  java
  • Delphi 中的哈希表(2): TStringHash

    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));  //90end;
    
    //修改、删除、清空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.
    
  • 相关阅读:
    ACM题目————STL练习之求次数
    ACM题目————STL + 全排列
    ACM题目———— 一种排序(STL之set)
    ACM第二站————STL之stack
    ACM题目————中位数
    ACM题目————Sunscreen
    ACM题目————区间覆盖问题
    ACM题目————困难的串
    ACM题目————A Knight's Journey
    ACM题目————Face The Right Way
  • 原文地址:https://www.cnblogs.com/huangjacky/p/1845749.html
Copyright © 2011-2022 走看看