集合中的计数、长度、大小

发布于 2024-07-09 06:27:09 字数 277 浏览 3 评论 0原文

通过使用多种编程语言和库,我注意到用于表示集合中元素总数的各种术语。

最常见的似乎是长度计数大小

例如。

array.length
vector.size()
collection.count

有没有什么首选术语可以使用? 这取决于它是什么类型的集合吗? IE。 可变/不可变

是否更倾向于将其作为属性而不是方法?

From using a number of programming languages and libraries I have noticed various terms used for the total number of elements in a collection.

The most common seem to be length, count, and size.

eg.

array.length
vector.size()
collection.count

Is there any preferred term to be used?
Does it depend on what type of collection it is? ie. mutable/immutable

Is there a preference for it being a property instead of a method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

蓝梦月影 2024-07-16 06:27:09

Length() 倾向于引用连续的元素 - 例如,字符串具有长度。

Count() 倾向于引用松散集合中的元素数量。

Size() 倾向于指集合的大小,通常这可能与向量(或字符串)等情况下的长度不同,字符串中可能有 10 个字符,但存储是保留的20。它也可能指元素的数量 - 检查来源/文档。

Capacity() - 用于专门指集合中分配的空间,而不是其中有效元素的数量。 如果类型同时定义了“容量”和“大小”,则“大小”通常指实际元素的数量。

我认为主要的一点是人类语言和习语,字符串的大小似乎不是很明显,而集合的长度同样令人困惑,即使它们可能用于指代相同的事物(元素的数量) )在数据集合中。

Length() tends to refer to contiguous elements - a string has a length for example.

Count() tends to refer to the number of elements in a looser collection.

Size() tends to refer to the size of the collection, often this can be different from the length in cases like vectors (or strings), there may be 10 characters in a string, but storage is reserved for 20. It also may refer to number of elements - check source/documentation.

Capacity() - used to specifically refer to allocated space in collection and not number of valid elements in it. If type has both "capacity" and "size" defined then "size" usually refers to number of actual elements.

I think the main point is down to human language and idioms, the size of a string doesn't seem very obvious, whilst the length of a set is equally confusing even though they might be used to refer to the same thing (number of elements) in a collection of data.

旧故 2024-07-16 06:27:09

FWIW(这几乎为零),我更喜欢“计数”,因为它似乎表明它将非常明确地返回集合中元素/项目的数量。

当面对术语“长度”或“大小”时,我经常会想一会儿(甚至被迫重新阅读文档)这该死的东西是否会告诉我集合中有多少元素或如何集合消耗了许多字节。 对于数组或字符串等连续的集合尤其如此。

但是没有人负责 Java、BCL/.Net 或 C/C++ 标准框架/库所使用的命名约定来询问我,所以你们都被他们想出的任何东西所困扰。

如果我比我聪明得多并且被命名为 Bjarne,那么你们所有人都可能免受痛苦......

当然,回到现实世界,您应该尝试坚持语言/平台使用的任何命名约定您正在使用(例如,C++ 中的 size())。 这似乎并不能帮助您解决 Array.Length 困境。

FWIW (and that's vanishingly close to nothing), I prefer 'Count' because it seems to indicate that it's going to return the number of elements/items in the collection pretty unambiguously.

When faced with the terms 'Length' or 'Size' I'm often left wondering for a moment (or even being forced to re-read documentation) whether the damn thing is going to tell me how many elements are in the collection or how many bytes the collection is consuming. This is particularly true for collections that are intended to be contiguous like arrays or strings.

But no one who was responsible for the naming conventions used by the Java, BCL/.Net, or C/C++ standard frameworks/libraries bothered to ask me, so you're all stuck with whatever they came up with.

If only I were much smarter than I am and was named Bjarne, all of you might be spared the misery...

Of course, back in the real world, you should try to stick with whatever naming convention is used by the language/platform you're using (eg., size() in C++). Not that this seems to help you with your Array.Length dilemma.

墟烟 2024-07-16 06:27:09

这些术语在某种程度上可以互换,尽管在某些情况下我更喜欢其中一个。 ,您可以获得最佳用法

通常,如果您考虑您将如何向另一个人口头描述该元素的长度/大小/计数? length() 暗示该元素 有一个长度。 字符串有长度。 你说“一个字符串有20个字符长”,对吧? 所以它有一个长度。

size() 暗示元素有一个大小。 例如,文件有一个大小。 你说“这个文件大小为 2 MB”,对吧? 所以它有一个尺寸。

也就是说,字符串也可以有大小,但我希望这里有其他东西。 例如,UTF-16 字符串的长度可能为 100 个字符,但由于每个字符都由两个字节组成,因此我预计大小为 200。

count() 非常不寻常。 Objective-C 使用 count 来计算数组中的元素数量。 有人可能会争论数组是否有长度(如在 Java 中)、有大小(如在大多数其他语言中)或有计数。 但是,大小可能又是字节大小(如果数组项是 32 位 int,则每个项是 4 个字节)和长度...我不会说“数组有 20 个元素长”,这听起来很奇怪我。 我会说“一个数组有 20 个元素”。 我不确定 count 是否很好地表达了这一点,但我认为 count 是 elementCount() 的缩写形式,这对于数组来说比 length()< 更有意义/code> 或 size()

如果您用编程语言创建自己的对象/元素,最好使用其他类似元素使用的任何内容,因为程序员习惯于使用该术语访问所需的属性。

The terms are somewhat interchangeably, though in some situations I would prefer one over another. Usually you can get the best usage if you think about How would you describe the length/size/count of this element verbally to another person?

length() implies that the element has a length. A string has a length. You say "a string is 20 characters long", right? So it has a length.

size() implies that the element has a size. E.g. a file has a size. You say "this file has a size of 2 MB", right? So it has a size.

That said, a string can also have a size, but I'd expect something else here. E.g. a UTF-16 string may have a length of 100 characters, but as every character is composed out of two byte, I'd expect size to be 200.

count() is very unusual. Objective-C uses count for the number of elements in an array. One might argue if an array has a length (as in Java), has a size (as in most other languages) or has a count. However, size might again be the size in byte (if the array items are 32 bit int, each item is 4 byte) and length... I wouldn't say "an array is 20 elements long", that sounds rather odd to me. I'd say "an array has 20 elements". I'm not sure if count expresses that very well, but I think count is here a short form for elementCount() and that again makes much more sense for an array than length() or size().

If you create own objects/elements in a programming language, it's best to use whatever other similar elements use, since programmers are used to accessing the desired property using that term.

风和你 2024-07-16 06:27:09

如果您要查找集合中的项目数量,我认为“计数”是最明显的术语。 对于还没有特别迷恋某种特定语言的新程序员来说,这一点甚至应该是显而易见的。

它应该是一个属性,因为它就是这样:集合的描述(也称为属性)。 方法意味着它必须对集合执行某些操作才能获取项目数量,而这似乎不直观。

Count I think is the most obvious term to use if you're looking for the number of items in a collection. That should even be obvious to new programmers who haven't become particularly attached to a given language yet.

And it should be a property as that's what it is: a description (aka property) of the collection. A method would imply that it has to do something to the collection to get the number of items and that just seems unintuitive.

等待我真够勒 2024-07-16 06:27:09

嗯...我不会使用尺寸。 因为这可能会与以字节为单位的大小混淆。
长度 - 对于数组来说可能有意义,只要它们应该使用后续的内存字节。
虽然……长度……在什么方面?
计数一目了然。 有多少个元素。 我会用计数。

关于属性/方法,我会使用属性来标记它快,使用方法来标记它慢。

而且,最重要的是 - 我会坚持您正在使用的语言/库的标准。

Hmm...I would not use size. Because this might be confused with size in bytes.
Length - could make some sense for arrays, as long as they are supposed to use consequent bytes of memory.
Though...length...in what?
Count is clear. How many elements. I would use count.

About property/method, I would use property to mark it's fast, and method to mark it's slow.

And, the most important - I would stick to the standards of the languages/libraries you are using.

我的鱼塘能养鲲 2024-07-16 06:27:09

添加@gbjbaanb的答案...

如果“属性”意味着对该值的公共访问,我会说“方法”是首选,只是为了提供封装并隐藏实现。

您可能会改变对如何计数元素或如何维护该计数的想法。 如果它是一个属性,那么您就会陷入困境 - 如果通过方法访问它,您可以更改底层实现,而不会影响集合的用户。

Adding to @gbjbaanb's answer...

If "property" implies public access to the value, I would say that "method" is preferred simply to provide encapsulation and to hide the implementation.

You might change you mind about how to count elements or how you maintain that count. If it is a property, you're stuck - if it is acessed via a method, you can change the underlying implementation without impacting users of the collection.

亚希 2024-07-16 06:27:09

Kotlin 回答

来自 _Collections.kt 的

/**
 * Returns the number of elements in this collection.
 */
@kotlin.internal.InlineOnly
public inline fun <T> Collection<T>.count(): Int {
    return size
}

Kotlin answer

from _Collections.kt

/**
 * Returns the number of elements in this collection.
 */
@kotlin.internal.InlineOnly
public inline fun <T> Collection<T>.count(): Int {
    return size
}
心在旅行 2024-07-16 06:27:09

在 Elixir 中,实际上有一个明确的命名方案与其跨语言类型相关联。

当“计算”数据结构中的元素数量时,Elixir
还遵守一个简单的规则:如果满足以下条件,则该函数被命名为 size
操作以恒定时间进行(即该值是预先计算的)或
length 如果操作是线性的(即计算长度得到
随着输入的增长而变慢)。

In Elixir there is actually a clear naming scheme associated with it across types in the language.

When “counting” the number of elements in a data structure, Elixir
also abides by a simple rule: the function is named size if the
operation is in constant time (i.e. the value is pre-calculated) or
length if the operation is linear (i.e. calculating the length gets
slower as the input grows).

缘字诀 2024-07-16 06:27:09

对我来说,这有点像问“foreach”是否比“foreach”更好。 这仅取决于语言/框架。

To me, this is a little like asking whether "foreach" is better than "for each". It just depends on the language/framework.

离去的眼神 2024-07-16 06:27:09

我想说,这取决于您使用的特定语言。 例如,在 C# 中,如果您使用 Array,则具有 Property Length,如果您有继承自 IEnumerable 的内容,则具有扩展 Method Count(),但速度并不快。 如果您从 ICollection 继承,您就有 Property 计数。

I would say that it depends on particular language that you are using and classes. For example in c# if you are using Array you have Property Length, if you have something that inherits from IEnumerable you have extension Method Count(), but it is not fast. And if you inherited from ICollection you have Property Count.

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