mercredi 15 septembre 2021

Difference between std::adjacent_find() and std::equal() for checking if all elements in vector are equal

I'm trying to figure out the difference between:

return std::adjacent_find(v.begin(), v.end(), std::not_equal_to<>()) == v.end();

and

return std::equal(v.begin() + 1, v.end(), v.begin());

The latter is bit shorter, so I'd prefer writing that, only difference I found being it crashes for empty arrays. Speed was about equal.

Aucun commentaire:

Enregistrer un commentaire