C++ STL Comparison class: how to parameterize comp-class behaviour?
i want to use a std::priority_queue container with a custom data-type and several comparison-criterions (i defined a functor for each of them; each of them are working on the same types). The comparison criterion itself used should be configurable through a function parameter (avoiding if_then_ initializations of the queue).
Based on this post, which is, as far as i understand it, using a comparison functor, which itself is initialized with a function pointer to one of the defined comparison functions, i tried to use a configurable comparison functor, which is initialized with another comparison functor (which is then stored locally and used when called). I did not get it to work yet.
I don't even know if it is possible to do what i want in general. I don't know if i did something wrong regarding the types (i used typedef boost::function xxx) or need additional C++0x features (would "closures" help?).
So basically i want to have something like the following to be working (this won't compile with an non-long but ugly error; GCC 4.5):
#include <iostream>
#include <queue>
#include <boost/tuple/tuple.hpp>
#include <boost/function.hpp>
typedef boost::tuple<double, int> custom_type; // example only!
typedef boost::function<bool (const custom_type & l, const custom_type & r)> comp_type; // comparison functor
struct fifo_comp
{
bool operator() (const custom_type & l, const custom_type & r) const
{
// compare only id
return l.get<1>() > r.开发者_Python百科get<1>();
}
};
struct lifo_comp
{
bool operator() (const custom_type & l, const custom_type & r)
{
// compare only id
return l.get<1>() < r.get<1>();
}
};
class Compare
{
public:
explicit Compare(const comp_type & comp);
bool operator() (const custom_type & l, const custom_type & r)
{
return comp(l, r);
}
private:
const comp_type & comp;
};
class Some_Class_Using_Queue
{
public:
Some_Class_Using_Queue(comp_type & comp) : pqueue(Compare(comp)) {}
void test()
{
pqueue.push(custom_type(1.0, 1));
}
private:
std::priority_queue<custom_type, std::vector<custom_type>, Compare> pqueue;
};
int main()
{
comp_type f = fifo_comp();
Some_Class_Using_Queue temp((f)); // critical
temp.test();
return 1;
}
- Any help to get it to work for me (use of STL/Boost is encouraged)?
- Any better ideas (do it completely different)?
- Are there any cool C++0x features helping here (maybe closures)?
Thank you.
PS/Edit: I know, that it is possible and really easy to make the class templated and call the class with the appropriate comp-class as template-param. But i don't know if that's a better way to do it (regarding design). With this approach i would have to switch/if-else before calling because of the need of the template param to be available at compile-time.
PS: I setthe "priority_queue" tag, even if the question is maybe completely independent on this adaptor-class. If someone wants to remove it, just do it.
You are missing the definition of Compare
's constructor:
explicit Compare(const comp_type & comp) : comp(comp) { }
Other than that, it builds and runs for me.
Also, Compare
appears to be redundant. It just wraps comp_type with an identical interface. The code still builds and runs after removing the Compare
class and replacing it with comp_type
everywhere.
精彩评论