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

  • A+
Category:Languages

This question already has an answer here:

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?

 


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).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: