C++20 added the method contains() to associative and unordered containers. This method checks for the presence of an element, simplifying the typical pattern of invoking find() and comparing against the end iterator. Compiler Explorer link: https://lnkd.in/dGzcV9KT #cpp #cplusplus #coding #programming #dailybiteofcpp
I think i is may be useful when you simply check for an absence of the element. But if for example I want to get the key value form a map I also need to find the element again (via at() or []). The pattern with auto iter = dict.find(...); iter->second ... does the job faster a bit.
Took them long enough
Is there the generic std::contains(c1, 0)? [as per c1.begin() vs. std::begin(c1)] If not, why not?
SUKRA HELITEK INC•9K followers
2yIs there a conditional bounds checking accessor method for std::vector? at() provides bounds checking, but it's slow for performance critical codes. operator [ ] is extremely fast, but I have continuously introduced out of bounds access bugs using it. A best solution would be something that allows bounds checking to be enabled/disabled at compile time. I didn't find anything in STL, so I'm just thinking of implementing something myself.