请考虑类A
和函数对象A_less
。 A_less
比较两个 A
指针,具体取决于 A::getvalue()
的结果。
class A {
int getvalue(const string &Parameter);
};
struct A_less : public binary_function<A *, A *, bool> {
A_less(const string &P) : Parameter(P) { }
bool operator()(const A *lhs, const A *rhs) const {
return A->getvalue(Parameter) < rhs->getvalue(Parameter);
}
string Parameter;
}
我如何声明/创建A
指针的排序容器(集合,priority_queues,...),根据特定的(运行-)按A_less
排序时间)参数
的值?
最佳答案
像这样:
std::string p = ...;
std::set<A,A_less> m(A_less(p));
您必须指定模板参数Compare
(这是set
的第二个参数)。构造map时,需要将比较函数对象交给map
的构造函数。
关于c++ - 在模板实例化中使用运行时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23061733/