存储桶列表中存储的项目数

发布于 2024-07-18 06:45:45 字数 332 浏览 11 评论 0原文

我想知道如何获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁微风不噪 2024-07-25 06:45:45

图书馆永远不会完整。 但你可以扩展它们。

如果您经常需要这个值,您可以为此编写一个类帮助器。

TBucketListHelper = class helper for TBucketList
  function GetCount: Integer;
end;

function TBucketListHelper.GetCount: Integer;
var
  i : Integer;
begin
  Result := 0;
  for I := 0 to BucketCount - 1 do
    Inc (Result, Buckets [I].Count);
end;

您现在可以使用:

BucketList.GetCount

如果 TBucketListHelper 在范围内。

Libraries are never complete. But you can extend them.

If you need this value often, you can write a class helper for that.

TBucketListHelper = class helper for TBucketList
  function GetCount: Integer;
end;

function TBucketListHelper.GetCount: Integer;
var
  i : Integer;
begin
  Result := 0;
  for I := 0 to BucketCount - 1 do
    Inc (Result, Buckets [I].Count);
end;

You now can use:

BucketList.GetCount

If TBucketListHelper is within scope.

李不 2024-07-25 06:45:45

没有其他解决方案无需使计数器与内容保持同步。
它基本上是一个集合的集合。

There is no other solution without having to keep a counter in sync with the content.
It's basically a collection of collections.

自此以后,行同陌路 2024-07-25 06:45:45

由于您使用的是自己的派生类,因此只需增加和减少添加删除上的计数器即可。 您可以包含循环方法作为双重检查,以防万一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文