set<int> s;
s.insert(1);
s.insert(2);
...
s.insert(n);
I wonder how much time it takes for s.find(k)
where k
is a number from 1..n?
I assume it is log(n). Is it correct?
set<int> s;
s.insert(1);
s.insert(2);
...
s.insert(n);
I wonder how much time it takes for s.find(k)
where k
is a number from 1..n?
I assume it is log(n). Is it correct?
O( log N ) to search for an individual element.
§23.1.2 Table 69
The complexity of
std::set::find()
beingO(log(n))
simply means that there will be of the order oflog(n)
comparisons of objects stored in theset
.If the complexity of the comparison of 2 elements in the set is
O(k)
, then the actual complexity, would beO(log(n)∗k)
.this can happen for example in case of set of strings (k would be the length of the longest string) as the comparison of 2 strings may imply comparing most of (or all) of their characters (if they start with the same prefix or are equal)
The documentation says the same: