Search code examples
c++algorithmstlstl-algorithm

Using static vs. member find method on a STL set?


I am using a set because, i want to use the quick look up property of a sorted container such as a set. I am wondering if I have to use the find member method to get the benefit of a sorted container, or can I also use the static find method in the STL algorithms?

My hunch is that using the static version will use a linear search instead of a binary search like I want.


Solution

  • You are right that the non-member version does a linear search, while the member version will do a O(log N) search. std::set is optimized for O(log N) insertion, retrieval and deletion.

    As a point of definition, the std::find method is not a static function. See here for a description of the various things static can mean in C++.