2012년 10월 19일 금요일

제네릭 이해를 돕기 위한 간단한 사용(TDictionary)

Generics 를 이해하기 위하여 Generics.collections 에 있는 클래스를 사용하면서 익히고 있습니다.
TDictionary 는 사전이라는 의미로 보면 좋겠습니다. 표제어(keys)가 있고 그 내용 설명(Values)이 있는 형식이요. 아래그림으로 이해에 도움이되시기를...
선언하고
var
  Form3: TForm3;
  iDictionary: TDictionary<string, integer>;
implementation

{$R *.dfm}

임의의 값으로 채워 출력하고
procedure TForm3.Button1Click(Sender: TObject);
var
  idx: integer;
  iStr: string;
  iKey: string;
  iValue: integer;
begin
  for idx := 0 to 19 do begin
    iValue := Random(65000);
    iKey := 'Key' + IntToStr(iValue);
    // if not iDictionary.ContainsKey(iKey) then iDictionary.Add(iKey, iValue);
    iDictionary.AddOrSetValue(iKey, iValue);
  end;
  for iStr in iDictionary.Keys do begin
    Memo1.Lines.Add(iStr + '=' + IntToStr(iDictionary.Items[iStr]));
  end;
end;

소팅하고 결과를 출력(소팅이 약간 이상하다?)
procedure TForm3.Button2Click(Sender: TObject);
var
  iStr: string;
  iArray: Tarray<string>;
begin
  iArray := iDictionary.Keys.ToArray;
  Tarray.Sort<string>(iArray);
  memo1.Lines.Add('Sorted');
  for iStr in iArray do begin  // iArray 임에 주의
    Memo1.Lines.Add(iStr+'='+IntToStr(iDictionary.Items[iStr]));
  end;
end;

생성하고 해제하고
procedure TForm3.FormCreate(Sender: TObject);
begin
  iDictionary := TDictionary<string, integer>.Create;
end;

procedure TForm3.FormDestroy(Sender: TObject);
begin
  iDictionary.Free;
end;

댓글 1개:

Unknown :

del2007 로만 사용하다 del2010 을 사용을 사용하니 이런 기능도 제공을 해주네요^^

tensorflow gpu 사용하기에서

 tensorflow 설치시 주의해야 한다. # Anything above 2.10 is not supported on the GPU on Windows Native python - m pip install "tensorflow<2.11...