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

delphi – 在将TDictionary排序成数组后处理TDictionary的正确方

发布时间:2020-12-15 09:30:12 所属栏目:大数据 来源:网络整理
导读:我有像TDictionary这样的 target_results : TDictionarylongint,double; 填充后我需要对结果进行排序.我是这样做的 type TSearchResult = TPairlongint,double;var target_results_array : TArrayTSearchResult;target_results_array:= target_results.ToArr
我有像TDictionary这样的

target_results : TDictionary<longint,double>;

填充后我需要对结果进行排序.我是这样做的

type
  TSearchResult = TPair<longint,double>;

var
 target_results_array : TArray<TSearchResult>;

target_results_array:= target_results.ToArray;
TArray.Sort<TSearchResult>(best_knowledge_search_results,TComparer<TSearchResult>.Construct(
                              function(const L,R: TSearchResult): Integer
                              begin
                                if L.Value < R.Value then Result := 1 else if L.Value > R.Value then Result := -1 else Result := 0;
                              end
                    ));

这一切都按预期工作.我的问题是如何在没有任何泄漏的情况下处理TDictionary和TArray?目前我正在做

target_results.Free;

解决方法

由于您的数据是longint和double,两者都是值类型,因此释放字典将正常工作.您的数组将包含原始值的副本,您无需担心丢失或损坏任何内容.

如果您有对象类型,并且您拥有一个拥有对象的TObjectDictionary,那么您必须担心这类事情,但对于纯数字数据,您没问题.

(编辑:李大同)

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

    推荐文章
      热点阅读