C++ STL 比较类:如何参数化比较类行为?
我想使用带有自定义数据类型和几个比较标准的 std::priority_queue 容器(我为每个标准定义了一个函子;每个标准都处理相同的类型)。 使用的比较标准本身应该可以通过函数参数进行配置(避免队列的 if_then_ 初始化)。
基于这篇文章,如下据我了解,使用比较函子,它本身是用指向定义的比较函数之一的函数指针初始化的,我尝试使用可配置的比较函子,它是用另一个比较函子初始化的(然后存储在本地并调用时使用)。我还没有让它发挥作用。
我什至不知道是否可以做我想做的事情。我不知道我是否在类型方面做错了(我使用了 typedef boost::function xxx)或者需要额外的 C++0x 功能(“闭包”有帮助吗?)。
所以基本上我希望有类似下面的东西可以工作(这不会编译一个不长但丑陋的错误;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.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;
}
- 任何帮助让它为我工作(使用STL/Boost)受到鼓励)?
- 有更好的想法(完全不同吗)?
- 是否有任何很酷的 C++0x 功能在这里提供帮助(也许是闭包)?
谢谢。
PS/编辑:我知道,使类模板化并使用适当的 comp-class 作为模板参数来调用该类是可能的,而且非常容易。但我不知道这是否是更好的方法(关于设计)。使用这种方法,我必须在调用之前切换/if-else,因为需要模板参数在编译时可用。
PS:我设置了“priority_queue”标签,即使问题可能完全是这样独立于该适配器类。如果有人想删除它,就这么做吧。
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.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少
Compare
构造函数的定义:除此之外,它为我构建和运行。
此外,
Compare
似乎是多余的。它只是用相同的接口包装 comp_type 。删除Compare
类并将其替换为comp_type
后,代码仍然可以构建并运行。You are missing the definition of
Compare
's constructor: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 theCompare
class and replacing it withcomp_type
everywhere.