bool hasId(string id, vector<User>& map)
{
User ans = *(find_if(map.begin(), map.end(), [&](User d)
{ return (id==(d).uid());}));
return ans.uid() == id;
}
bool hasId(string id, vector<User>& map) { User ans = *(find_if(map.begin(), map.end(), [&](User d) {
Share
If no matches are found then
find_ifreturnslast(), in your case,map.end().end()does not return a valid iterator (it’s one past the last element), yet you assume that a match is always found and proceed to dereference the return value unconditionally. This is a problem.You need to perform a check before the dereference. Without the check your function simply assumes that a match is always found as
ans.uid() == idis the predicate tofind_if(and thus redundant), so you may as well just replace the entire thing withreturn true;as it stands 🙂On a side note, it’s kinda weird to call a
vectoramap, confusing at least.Documentation for find_if
Relevant bit: