Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why std::map has a member function called count? [duplicate]

Tags:

c++

std

stdmap

I'm learning C++ and apparently a way of checking if a particular key exists in a std::map is using the member function count.

My first though was: Aren't the keys supposed to be unique? And checking the documentation indeed they are unique, so count will either return 0 or 1.

Isn't it a bit counter-intuitive to call it count? Why not exist?

To me count makes sense in a list where you expect a number of occurrences of an element, but if the method is only allowed to return 1 or 0 it doesn't make sense to me.

Am I missing something? Is there a reason to call it count or it's just a bad naming?

like image 377
Sembei Norimaki Avatar asked Dec 04 '18 14:12

Sembei Norimaki


1 Answers

It's a little like using a method .numberOfWives() to determine if you are married or not. Does the job and helps build generic code.

Sure, it's normally 0 or 1 (std::map), but it could be more than one (std::multimap, or polygamous jurisdictions).

like image 171
Bathsheba Avatar answered Oct 16 '22 12:10

Bathsheba