[英]Why do unordered_set operations like count and erase return a size_type?
Apparently, unordered_set::erase
and unordered_set::count
return something that is not strictly boolean (logically, that is, I'm not talking about the actual type). 显然, unordered_set::erase
和unordered_set::count
返回的东西不是严格的布尔值(逻辑上,也就是说,我不是在谈论实际的类型)。
The linked page reads for the third version of erase: 链接页面读取第三版擦除:
size_type erase( const key_type& key );
Removes the elements with the key value key 使用键值键删除元素
This has a tone to it that suggests there could be more than just one element with a given key. 这有一个基调,表明可能不只有一个元素与给定的键。 It doesn't explicitly state this, but it sounds like it a lot. 它没有明确说明这一点,但听起来很多。
Now, the point of a set, even an unordered one, is to have each element once. 现在,一个集合的点,即使是无序集合,也是每个元素一次。
The standard library acknowledges the existence of the bool
type and uses it for boolean values like unordered_set::empty()
. 标准库确认存在bool
类型,并将其用于布尔值,如unordered_set::empty()
。 So, what's the point of returning size_type
in the cases above? 那么,在上述情况下返回size_type
有什么意义呢? Even in spite of hash collisions, the container should distinguish elements with different keys, right? 即使存在哈希冲突,容器也应该区分具有不同键的元素,对吧? Can I still rely on that? 我还能依靠吗?
a.erase(k) size_type Erases all elements with key equivalent to k. a.erase(k)size_type用等于k的键擦除所有元素。 Returns the number of elements erased. 返回已擦除元素的数量。
b.count(k) size_type Returns the number of elements with key equivalent to k. b.count(k)size_type返回密钥等于k的元素个数。
It's because of the unordered associative container requirements [23.2.5]. 这是因为无序的关联容器要求 [23.2.5]。
It's probably just so that they could re-use the wording from unordered_multiset
. 它可能只是为了让他们可以重复使用unordered_multiset
的措辞。 You don't have to worry about hash collisions except for performance-wise, the container is still correct even if every element collides- even if such a thing would be stupendously slow. 您不必担心散列冲突,除了性能方面,即使每个元素发生碰撞,容器仍然是正确的 - 即使这样的事情会非常缓慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.