Why is the C++ STL set container's count() method thus named?

前端 未结 3 1652
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 03:13

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 t

3条回答
  •  粉色の甜心
    2020-12-11 03:48

    All associative containers must meet the requirements listed in §23.2.4/8 Table 102 - Associative container requirements. One of these is that they implement a.count(k) which then

    returns the number of elements with key equivalent to k

    So the reason is to have a consistent interface between all associative containers. For instance, this uniformity will be very important when writing generic function templates that must work with any associative container.

提交回复
热议问题