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

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

问题描述

从使用多种编程语言和库我已经注意到用于集合中的元素总数的各种术语。



最常见的似乎是 length count size 。 b
$ b

例如

  array.length 
vector.size $ b collection.count

是否有任何首选术语?
它取决于什么类型的集合是什么?即。 mutable / immutable



是否有偏好是属性而不是方法?

解决方案

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



Count()往往指的是较宽松集合中的元素数。



Size()往往指的是集合的大小,通常这可能不同于例如向量(或字符串),字符串中可能有10个字符,但是存储空间保留为20.它也可以引用元素数 - 检查源/文档。



Capacity() - 用于专门引用集合中的已分配空间,而不是其中有效元素的数量。如果类型同时具有capacity和size,那么size通常是指实际元素的数量。



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


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?

解决方案

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.

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

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