简体   繁体   English

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

[英]Correct way to dispose of TDictionary after sorting it into an array

I have a TDictionary like 我有像TDictionary这样的

target_results : TDictionary<longint,double>;

After populating it I need to sort the results. 填充后我需要对结果进行排序。 I'm doing it like this 我是这样做的

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
                    ));

It's all working as expected. 这一切都按预期工作。 My question is how do I dispose of the TDictionary and the TArray without any leaks? 我的问题是如何在没有任何泄漏的情况下处理TDictionary和TArray? Currently I'm just doing 目前我正在做

target_results.Free;

Since your data is longints and doubles, which are both value types, freeing the dictionary will work fine. 由于您的数据是longint和double,两者都是值类型,因此释放字典将正常工作。 Your array will contain a copy of the original values, and you don't need to worry about losing or corrupting anything. 您的数组将包含原始值的副本,您无需担心丢失或损坏任何内容。

If you had object types instead, and you had a TObjectDictionary that owned the objects, then you'd have to worry about this sort of thing, but for purely numeric data, you're fine. 如果您有对象类型,并且您拥有一个拥有对象的TObjectDictionary ,那么您必须担心这类事情,但对于纯数字数据,您没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM