delphi學習筆記(1)尋找演算法

來源:互聯網
上載者:User
delphi 尋找演算法

順序尋找   數組

type
    //如果兩數相等 返回0 如果第一個小於第二個 返回負數 否則 返回正數
    TCompareFunc=function (data1,data2:Pointer):integer;

function CompareLongint(data1,data2:Pointer):integer;
var
    L1: Lingint absolute data1;
    L2: Lingint absolute data2;
begin
    if L1=L2 then
        result:=0;
    else if L1<L2 then
        result:=-1;
    else result:=1;
end;

//比較兩個以null結尾的字串
function CompareNullStr(data1,data2:Pointer):integer;
begin
    Result:=StrComp(PAnsiChar(data1), PAnsiChar(data2));
end;

procedure Find(aList:TList;aItem:Pointer;aComparer:TCompareFunc):integer;
begin
    for i:=0 to pred(aList.Count) do
        if aComparer(aList.List^[i],aItem)=0 then begin
           Result:=i;
            Exit;
        end;
    Result:=-1;
end;

總結:
1 object pascal規定 迴圈正常結束的話,迴圈計數器的值是未定義的,相反,如果過早退出,則是已定義了的。
2 使用aList.List^[i]比用List[i]有效率,因為list[i]實際上是調用一個函數TList.Get,它會檢查i的值是否超出範圍,否則拋出異常。
3 absolute 的作用   建立兩個地址相同的變數。

總結:
1 object pascal規定 迴圈正常結束的話,迴圈計數器的值是未定義的,相反,如果過早退出,則是已定義了的。
2 使用aList.List^[i]比用List[i]有效率,因為list[i]實際上是調用一個函數TList.Get,它會檢查i的值是否超出範圍,否則拋出異常。
3 absolute 的作用   建立兩個地址相同的變數。

正在寫。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.