为什么这样命名C ++ STL设置容器的count()方法? [英] Why is the C++ STL set container's count() method thus named?

查看:173
本文介绍了为什么这样命名C ++ STL设置容器的count()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它真正检查的是contains()而不是出现次数的计数,对吗?也不允许重复,所以contains()会比count()更好吗?

解决方案

考虑到多态性的重要方面之一是能够使用相同的API处理不同的类,这是使其与其他容器类一致. /p>

确实返回计数.一组计数只能为零或一个的事实不会改变该方面.

与基本上只允许两个值"同时包含两个对象的收集对象没有什么根本区别.在那种情况下,它将返回零,一或两个的计数,但它仍然是一个计数,与集合相同.

标准中与此相关的部分是C++11 23.2.4,它讨论了关联容器setmultisetmapmultimap.表102包含对这些关联容器的要求,而不是对常规"容器的要求,count的位解释如下:

size_type a.count(k)-返回键等效于k的元素数.复杂度是log(a.size()) + a.count(k).

What it really checks for is contains() and not the count of the number of occurrences, right? Duplicates are not permitted either so wouldn't contains() be a better name than count()?

解决方案

It's to make it consistent with other container classes, given that one of the great aspects of polymorphism is to be able to treat different classes with the same API.

It does actually return the count. The fact that the count can only be zero or one for a set does not change that aspect.

It's not fundamentally different to a collection object that only allows two things of each "value" at the same time. In that case, it would return the count of zero, one or two, but it's still a count, the same as with a set.

The relevant part of the standard that requires this is C++11 23.2.4 which talks about the associative containers set, multiset, map and multimap. Table 102 contains the requirements for these associative containers over and above the requirements for "regular" containers, and the bit for count is paraphrased below:

size_type a.count(k) - returns the number of elements with key equivalent to k. Complexity is log(a.size()) + a.count(k).

这篇关于为什么这样命名C ++ STL设置容器的count()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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