哪个 Delphi 数据结构可以保存唯一整数列表?
当我处理 Java 问题时,我使用集合模式。然而,在 Delphi 中这样做是一场噩梦,因为没有 Integer 对象来处理事情。
我需要一个保存数字的数据结构。我希望能够添加数字、删除数字以及检查集合的内容,并且每个数字必须是唯一的。
我对需要自己实施和测试错误的解决方案不感兴趣。有没有像Java的HashTable这样现成的对象?
When I approach Java problems, I use the collection pattern. However, doing it in Delphi is quite a nightmare since there is no Integer object to handle things.
I need a data structure that holds numbers. I want to be able to add numbers, remove numbers, and check the contents of the collection, and each number must be unique.
I'm not interested in a solution I need to implement and test for bugs myself. Is there a ready object like Java's HashTable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
GpLists 附带 BSD 许可证。它还包含一个保存 64 位整数的类 - TGpInt64List - 以及一堆其他东西。
GpLists comes with a BSD license. It also contains a class holding 64-bit integers - TGpInt64List - and bunch of other stuff.
Dictionary
或类似的就可以了。Dictionary<Integer,Boolean>
or similar would do.我知道它很脏,但您可能会滥用
TStringList
(或THashedStringList
)。I know it's dirty, but you could misuse
TStringList
(orTHashedStringList
).这是带有泛型的 Delphi 版本的简单解决方案:
如果大量整数的性能很重要,那么您可以保持列表排序并使用二进制搜索
This is a simple solution for the Delphi version with generics:
And if performance with lots of integers is important then you can keep the list sorted and use the binary serch
“标准”VCL 库中的 Delphi 容器类很差。这是一个长期存在的问题,在最新版本中仅部分得到纠正。
如果您使用的是 Delphi >= 2009,那么您拥有也可以处理整数数据类型的泛型类,然后您必须编写自己的类、以非标准方式使用 TList 或使用第三方库。
如果你必须存储数字,如果它们最多 32 位长,你可以将它们存储在 TList 中,将它们转换为指针或从指针转换。您必须重写 Add() 方法以确保唯一性。您还可以使用 TBits 并将相应的“槽”设置为 true。
否则,您需要使用第三方库,例如 JCL (免费)或 DIContainers(商业)。
Delphi containers class in the "standard" VCL library are poor. This is a long standing issue only partially corrected in latest versions.
If you are using Delphi >= 2009 you have generics class that can handle integer data types as well, before you have to write your own class, use TList in a non standard way, or use a third party library.
If you have to store numbers, if they are at most 32 bit long you can store them in a TList, casting them to and from pointers. You have to override the Add() method to ensure uniqueness. You could also use TBits and set to true the corresponding "slot".
Otherwise you need to use third party libraries like the JCL (free) or DIContainers (commercial), for example.
您可以使用 TList 来存储一组整数。它应该存储指针,但由于指针只是整数,因此在存储整数时它可以完美地工作。
You can use a TList to store a set of integers. It is supposed to store Pointers, but since Pointers are just integers it works perfectly when storing Integers.
Delphi有一个单元mxarrays(Decision Cube),有一个类TIntArray,设置它的属性Duplicates为dupIgnore。它还可以对值进行排序。如果您要使用它,请参阅质量中心报告#:2703进行更正该单元中的错误。
Delphi has unit mxarrays (Decision Cube), there is a class TIntArray, set it's property Duplicates to dupIgnore. It's also can sort values. If you will use it, see Quality Central Report #:2703 to correct the bug in this unit.
就个人而言,我强烈建议开始使用 DeCAL 来存储数据。它有 DMap 容器,可以处理几乎任何数据类型,是自我优化的,因为它使用内部红黑树,并且不允许您添加重复项(如果需要插入重复项,可以使用 DMultiMap)。 DMap 的另一个优点是在列表中查找元素非常快(比在 TStringList 中快得多)。使用 DeCal 与使用其他 Delphi 库有点不同,但是一旦您熟悉了它,您就不会在代码中使用任何 StringList。
编辑:旧版本的 DeCAL 位于 SourceForge 上,但在这里您可以找到很棒的 pdf 手册。
Personally, i strongly recommend start using DeCAL for storing data. It has DMap container which can handle almost any data type, is self optimized because it uses internal Red-Black tree and it won't allow you to add duplicates (if you need to insert duplicates, you can use DMultiMap). Another great thing with DMap is that finding element in the list is very fast (much faster than in TStringList). Working with DeCal is a bit different than with other Delphi libraries but once you get comfortable with it, you won't use any StringList in your code.
Edit: older version of DeCAL is on SourceForge, but here you can find great pdf manual.
是的,有,它叫做 TDictionary
Yes, there is, it's called TDictionary