On Thursday, 1 August 2019 11:18:33 UTC+3, Fraser Ross wrote:
> On 01/08/2019 06:44, Jorgen Grahn wrote:
> > On Wed, 2019-07-31, Öö Tiib wrote:
> > ...
> >> Note that contains() is C++2020 map feature and C++2020 standard does
> >> not exist yet. I don't even understand why to add it since it does
> >> exactly same thing that std::map::count() already does.
> >
> > Probably to help us write more readable code:
> >
> > if (employees.count(someone)) fire(someone);
Note that in actual code it can have non-confusing form like:
if (employees.count(someone)!=0) fire(someone);
> > if (employees.contains(someone)) fire(someone);
> >
> > The latter reads better IMO. On the other hand, I think in 90% of
> > cases you want to find() the element so you can access it if it
> > exists.
> >
> > /Jorgen
> >
>
> contains can stop after finding an equivalent element in a multimap or
> multiset.
Good catch!
Even there it can have same issue that empty() versus size()!=0 had.
The empty() is shorter and was cheaper for some containers but
programmers kept massively using size()!=0 (and its equivalents) so
committee had to require size() to be O(1) for all containers.
It is hard to figure why human brain works like that but programming
language has to support it to write efficient programs.