What is the time complexity of a find method in a set in C ++?

set<int> s;

s.insert(1);
s.insert(2);
...
s.insert(n);

I wonder how long it takes for s.find(k), where kis the number from 1..n? I assume this is log (n). Is it correct?

+5
source share
2 answers

O (log N) to search for a single item.

§23.1.2 Table 69

expression  return            note                                   complexity
a.find(k)   iterator;         returns an iterator pointing to an     logarithmic
            const_iterator    element with the key equivalent to k, 
            for constant a    or a.end() if such an element is not 
                              found
+13
source

The complexity std::set::find()being O(log(n))simply means that there will be a log(n)comparison order of the objects stored in set.

O(k), O(log(n)∗k).
  , , (k ), ( ) ( )

:

.
+1

All Articles