The C++ standard library provides std::equal_to
. This function object invokes operator==
on type T
by default.
What's the benefit of using std::equal_to
? Could you provide an example where std::equal_to
is useful?
To be used in algorithms. It provides a functor with operator() on it, and thus can be used generically.
Specific (and contrived) example, as asked in comments:
// compare two sequences and produce a third one // having true for positions where both sequences // have equal elements std::transform(seq1.begin(), seq1.end(), seq2.begin(), std::inserter(resul), std::equal_to<>());
Not sure who might need it, but it is an example.
Having std::equal_to
is very useful because it allows the equality comparison to be used as a functor, which means that it can be passed as an argument to templates and functions. This is something that isn't possible with the equality operator ==
since operators simply cannot be passed as parameters.
Consider, for example, how it can be used with std::inner_product
, std::find_first_of
and std::unordered_map
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With