集合中的计数 vs 长度 vs 大小 [英] count vs length vs size in a collection

查看:33
本文介绍了集合中的计数 vs 长度 vs 大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

最常见的似乎是lengthcountsize.

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

例如

array.length
vector.size()
collection.count

是否有任何首选术语可供使用?这是否取决于它是什么类型的集合?IE.可变/不可变

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?

推荐答案

Length() 倾向于指代连续的元素——例如,一个字符串有一个长度.

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

Count() 倾向于指代松散集合中元素的数量.

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

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

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() - 用于特指集合中分配的空间,而不是其中有效元素的数量.如果类型同时定义了容量"和大小",那么大小"通常是指实际元素的数量.

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.

这篇关于集合中的计数 vs 长度 vs 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆