c++ - use a run-time value in template instantiation -
please consider class a
, , function object a_less
. a_less
compares 2 a
pointers, depending on result of a::getvalue()
.
class { int getvalue(const string ¶meter); }; struct a_less : public binary_function<a *, *, bool> { a_less(const string &p) : parameter(p) { } bool operator()(const *lhs, const *rhs) const { return a->getvalue(parameter) < rhs->getvalue(parameter); } string parameter; }
how go declaring/creating sorted containers (sets, priority_queues, ...), of a
pointers, sorted a_less
depending on specific (run-time) values of parameter
?
like this:
std::string p = ...; std::set<a,a_less> m(a_less(p));
you have specify template parameter compare
(which 2nd set
). when constructing map, need give comparison function object constructor of map
.
Comments
Post a Comment