Search code examples
c++dictionarylanguage-features

Why is there no "exists" functions within C++ map?


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

Solution

  • Since C++20 you can use contains.

    Return value

    true if there is such an element, otherwise false.

    if(myMap.contains(item)) ...