I’m using a stl::set to keep elements sorted as they are inserted. My question is about the random access. If I have a complex class(difficult to init, for example), I found that is easy to insert, because I define the less operator into class. But if the key is the class itself, how do I need to access to the class? With a find()? I need to init a complex class only to find my class?
So my question is: how to random access to a set elements when elements are complex classes difficult to initialize?
Thanks
I don’t think it is possible : as you already noticed, the
std::set<>::findmember function expects aconst key_type &(which, in astd::setis identical tovalue_type).If you object is expensive to construct, chances are that you should better use a
std::map(which also is a sorted container), possibly with values being (smart) pointers on your type.