DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: qiaoguoqiang
今日帖子: 4
在线用户: 5
导航: 论坛 -> 移动应用开发 斑竹:flyers,iamdream  
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/7 18:46:39
标题:
新手请问函数使用不同泛型的问题 浏览:756
加入我的收藏
楼主: procedure Swap<t>(var A, B: t);
begin
  var C: t := A;
  A := B;
  B := C;
end;
//

procedure Sortarr<T1, T2>(var SourceArr: TList<T1>; var SortArr: TList<T2>; ascending: Boolean);
var
  i, j: Integer;
begin
  if ascending then
  begin
    for i := 0 to SourceArr.Count - 2 do
      for j := 0 to SourceArr.Count - 2 - i do
      begin
        if TValue.From<t1>(SourceArr[j]).tostring > TValue.From<t1>(SourceArr[j + 1]).tostring then
        begin
          Swap<t1>(SourceArr[j], SourceArr[j + 1]);
          Swap<t2>(SortArr[j], SortArr[j + 1]);
        end;
      end;
  end
  else
  begin
    for i := 0 to SourceArr.Count - 2 do
      for j := 0 to SourceArr.Count - 2 - i do
      begin
        if TValue.From<t1>(SourceArr[j]).tostring < TValue.From<t1>(SourceArr[j + 1]).tostring then
        begin
          Swap<t1>(SourceArr[j], SourceArr[j + 1]);
          Swap<t2>(SortArr[j], SortArr[j + 1]);
        end;
      end;
  end;
end;
//请问这样写为什么swap那里报错,如果把Tlist换成Tarray就不会报错
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/7 18:58:34
1楼: 原因是swap是地址交换 而tarray里面是存的地址 tlist里面存放的是值不是地址吗
----------------------------------------------
-
作者:
男 chencong5025 (Nicosoft) ▲▲▲△△ -
普通会员
2023/9/7 20:50:56
2楼: 不懂。好奇的是
Delphi 能在普通函数用泛型了?
----------------------------------------------
-
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/7 21:46:01
3楼: 为什么Tarray<T>可以传地址 Tlist<T>传地址报错 泛型有指针吗  求解答
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/7 22:24:35
4楼: this does not works?

xxxxx( @obj1, @obj2 ); --> "@" -> pointer of the objeto passed
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/7 22:37:13
5楼: @emailx45 not works

  procedure Swap<T>(var A: TList<T>; m, n: Integer);
改成这样才能不报错 不知道为什么
而且我发现函数不能同时使用inline;overload; static;来修饰,因为内联函数不能重载吗
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/7 23:44:56
6楼: var = object REFERENCE... then, it will be the object itself

"inline" < 32bytes  if im not wrong
...... the "code"  would be placed in all places where the func/proc was used...
in a regular func/proc, only the "reference/pointer" would be used (+/-) this
app size with many "inline" proc/func = > size

NOTE: there is not certainties about it, because depend of many things and the compiler may or may not be able to embed code in all places in the code where the function or procedure was used... the many conditions that will be evaluated
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/8 11:27:17
7楼: @emailx45 好的 谢谢
----------------------------------------------
-
作者:
男 bahamut8348 (leonna) ★☆☆☆☆ -
普通会员
2023/9/8 15:00:30
8楼: 哦?delphi里也可以支持泛型的静态函数了?
而且用tvalue.tostring来对比是很粗暴的做法,这种办法能支持的类型很少很少,哪怕是pchar这种类型都会出错。
----------------------------------------------
--
作者:
男 mp654kk (mp654kk) ▲△△△△ -
普通会员
2023/9/8 19:38:36
9楼: @bahamut8348 是的 我一般只用整型和字符串所以偷懒了 按道理是应该讨论类型
If TValue.From<T>(SourceArr[j]).Kind=ttypekind.tkFloat then......
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2023/9/9 4:27:33
10楼: and this way....?

TArray.SORT<....>( .... )  <-- can use many other ways to comparer and sort the data.

class procedure TArray.Sort<T>(var Values: array of T; const Comparer: IComparer<T>);


----------
unit Unit2;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Generics.Collections;

type
  TMyClass<T> = class { }
  private type
    TArr = TArray<T>;
    //
  public
    // class procedure SortMyArr(var Arr: TArr; const Comparer: IComparer<T> = nil); <-----
    class procedure SortMyArr(var Arr: TArr);
  end;

implementation

uses
  System.Rtti,
  System.TypInfo,
  System.Variants;

{ TMyClass }

//class procedure TMyClass<T>.SortMyArr(var Arr: TArr; const Comparer: IComparer<T> = nil);
class procedure TMyClass<T>.SortMyArr(var Arr: TArr);
var
  Ctx: TRttiContext;
  Typ: TRttiType;
  //
  LenArr: integer;
begin
  LenArr := length(Arr);
  if (LenArr < 2) then
    exit;
  //
  Ctx := TRttiContext.Create;
  Typ := Ctx.GetType(TypeInfo(T));
  //
  if (Typ <> nil) then
    begin
      case Typ.TypeKind of
        tkUString, tkInteger { simple types ... } :
          begin
          // TArray.Sort<T>(Arr, Comparer);
          TArray.Sort<T>(Arr);
          end;
        tkClass:
          begin
          // how to order it?
          end;
        // ...
      end;
    end;
end;

end.
----------

uses
  Unit2;

procedure TForm1.Button1Click(Sender: TObject);
var
  LArr: TArray<string>;
begin
  Memo1.Text := '';
  //
  LArr := ['hello', 'delphi', 'world', 'abc', 'ghi'];
  Memo1.Lines.AddStrings(LArr);
  //
  TMyClass<string>.SortMyArr(LArr);
  //
  Memo1.Lines.Add('--------');
  Memo1.Lines.AddStrings(LArr);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  LArr : TArray<TButton>;
  LText: string;
begin
  LArr         := [TButton.Create(Form1), TButton.Create(Form1), TButton.Create(Form1), TButton.Create(Form1)];
  LArr[0].Name := 'bt4';
  LArr[1].Name := 'bt2';
  LArr[2].Name := 'bt1';
  LArr[3].Name := 'bt3';
  //
  Memo1.Text := '';
  //
  for var B in LArr do
    LText := LText + slinebreak + B.Name;
  //
  Memo1.Lines.Add(LText);
  //
  TMyClass<TButton>.SortMyArr(LArr);
  //
  Memo1.Lines.Add('--------');
  //
  LText := '';
  for var B in LArr do
    LText := LText + slinebreak + B.Name;
  //
  Memo1.Lines.Add(LText);
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行80.07813毫秒 RSS