Custom comparator function

If, however, you would like to use another comparison function, for example, sorting or finding a string by length, a custom function can be provided as an additional argument. While the original algorithm uses a value (for example, std::find()), the version with a specific operator has the same name with _if attached at the end (std::find_if(...), std::count_if(...), and so on):

auto names = std::vector<std::string> {  "Ralph", "Lisa", "Homer", "Maggie", "Apu", "Bart"};std::sort(names.begin(), names.end(),   [](const std::string& a,const std::string& b){    return a.size() < b.size();  });// names is now "Apu", "Lisa", "Bart", "Ralph", "Homer", "Maggie" auto target_sz = size_t{3};auto x = std::find_if(names.begin(), ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.