Let us assume we have a map class (unordered map, list, set, whatever will also do). We are looking for a specific element. After calling the find() member, we have to check with the end() member. But find() internally already knows whether it is returning a good iterator or the end iterator. Why should we need to call end() again? This adds some overhead.
std::map<int,float> myMap;
// some other code to populate the map
// ...
std::map<int,float>::iterator myIt;
myIt = myMap.find(2); // doesn't this already know wheter its returning the end() iterator?
if (myIt != myMap.end()) { //calling end() here wastes some time because find
//already knew the result
std::cout << "Found, value is "<<(*myIt).second<<"\n";
} else {
std::cout << "Not found.\n";
}
There should be a way to know what the result of find() is without calling end().