[MultiIndex] comparison operator
Hi, I know that there are for sure important design decisions explaining why all std containers as well as multiindex take a comparison operator, as template parameter. This behavior is satisfactory as long, as comparison is sort of context independent. The best example say ints, but there exist cases when comparison of two object requires some priory knowledge about context this objects exist in. My favorite example is tenor ( time period ). It is impossible to say whether 1 month is less then 30 days unless you know when you start counting. So in order to compare such objects I need to specify some anchor date. Unfortunately I cannot have a std::set of such objects, nor a multiindex indexed with this objects. Note: I am able to sort a vector of such objects using std::sort. First question: Is there a way to use such objects with multiindex, if not why? Second question: I cannot be the only bloke disturbed by this issue, how are you dealing with it? Regards, Lukasz Dobrek
Hello Lukasz, Lukasz Dobrek ha escrito:
Hi, I know that there are for sure important design decisions explaining why all std containers as well as multiindex take a comparison operator, as template parameter. This behavior is satisfactory as long, as comparison is sort of context independent. The best example say ints, but there exist cases when comparison of two object requires some priory knowledge about context this objects exist in. My favorite example is tenor ( time period ).
tenor?
It is impossible to say whether 1 month is less then 30 days unless you know when you start counting. So in order to compare such objects I need to specify some anchor date. Unfortunately I cannot have a std::set of such objects, nor a multiindex indexed with this objects.
Note: I am able to sort a vector of such objects using std::sort.
How do you do that? An example with vector can help me better understand your problem.
First question: Is there a way to use such objects with multiindex, if not why?
Well, I'm not sure if I'm getting your question, but seems to me STL ordered containers and Boost.MultiIndex allows for context-dependent comparison operators: you just have to provide the contextual info as part of the comparison object internal state: class object_context_compare { public: object_context_compare(const context_info& info):info(info){} bool operator()(const object& x,const object& y)const { // compare x and y using info as the comparison context } private: context_info info; }; typedef std::set
Second question: I cannot be the only bloke disturbed by this issue, how are you dealing with it?
Hopefully answered above. Is this what you were after?
Regards, Lukasz Dobrek
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Well, I'm not sure if I'm getting your question, but seems to me STL ordered containers and Boost.MultiIndex allows for context-dependent comparison operators: you just have to provide the contextual info as part of the comparison object internal state:
This is the famous mailing list effect, 2 minutes after I send this post I looked again into the header of std::set just to find exactly what you described. I don't know how could I have missed it before. I was just all the time sure one cannot do it, so never really tried to look for it. Many Thanks anyway, Lukasz Dobrek
participants (2)
-
Joaquín Mª López Muñoz
-
Lukasz Dobrek