Is a log(n) std::find_if possible?

Viewed 903

I will define a class and use a container (possibly std::set) with a custom comparator function (basically, raw integer comparison on a class member variable). I wish to know if I can somehow keep this set std::sorted and use std::find_if with logarithmic performance instead of naive linear searching.

Thanks in advance.

1 Answers

std::set is an ordered container. It's already sorted with the comparator you provided. Just use its own std::set::find member function.

With C++14 and above, you can even adjust the comparator in order to allow find to work with any type that is comparable to your elements, and respects the same order.

Related