Say I have
std::set<classtype> set;
class classtype {
bool operator==(const classtype& ct) {
//..
}
};
//..
std::set<classtype>::iterator it = set.find(element);
Find does use the == operator from the class correct?
Also my reference says it has log(n) worst case runtime where n is the number of elements in the set. How is this realized internally? I understand that the key is that the elements in the set have a order (so insertion takes long to create that order), for integer sets it is clear what order means but for random classes not so much.
From the C++ Standard (23.2.4 Associative containers)
Member function
find
seeks a key according to the comparison objectcomp
If you did not specify explicitly the comparison object then the class uses by default standard functional object
std::less
that usesoperator <
within its operator function. So your class has to have the operator < defined.If you want to use
operator ==
for comparison values in the set then you can use standard algorithmstd::find
instead of the methodfind
.