存储桶列表中存储的项目数
我想知道如何获取 TBucketList 中存储的项目数。 据我所知,只有存储桶的数量和可用的存储桶数组,所以我能想到的就是
Count := 0;
for I := 0 to BucketList.BucketCount - 1 do
Inc (Count, BucketList.Buckets [I].Count);
这确实有效,但对我来说似乎很奇怪,我必须迭代所有存储桶才能找到项目的数量存储 - 可能经常需要的东西。
我是否错过了另一种可能性? 或者这个容器类没有像其他容器一样提供 Count 成员是否有原因?
谢谢!
I was wondering how to get the number of items stored in a TBucketList. As far as I can see there is only the number of buckets and the bucket array available, so all I can think of is
Count := 0;
for I := 0 to BucketList.BucketCount - 1 do
Inc (Count, BucketList.Buckets [I].Count);
That does work but it seems odd to me, that I have to iterate through all buckets to find the number of items stored - something that is probably needed very often.
Am I missing another possibility? Or is there a reason why this container class does not provide a Count member like the other containers?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
图书馆永远不会完整。 但你可以扩展它们。
如果您经常需要这个值,您可以为此编写一个类帮助器。
您现在可以使用:
如果 TBucketListHelper 在范围内。
Libraries are never complete. But you can extend them.
If you need this value often, you can write a class helper for that.
You now can use:
If TBucketListHelper is within scope.
没有其他解决方案无需使计数器与内容保持同步。
它基本上是一个集合的集合。
There is no other solution without having to keep a counter in sync with the content.
It's basically a collection of collections.
由于您使用的是自己的派生类,因此只需增加和减少添加和删除上的计数器即可。 您可以包含循环方法作为双重检查,以防万一。
Since you are using your own derived class then just increment and decrement a counter on Add and Remove. You could include the loop method as a double check just in case.