I find myself sometimes checking to see if an item already exists in a std::map
, which I do with the following:
if(myMap.find(item) != myMap.end()) ...
I was wondering why there is not a function such as exists()
which would return the same bool of whether the item is already in the map.
It would save a bit of typing, but more importantly it would seem to be far clearer:
if(myMap.exists(item)) ...
Since C++20 you can use contains
.
Return value
true
if there is such an element, otherwisefalse
.
if(myMap.contains(item)) ...