c++ - Custom comparator (ordering) as a (multi)map ordering parameter? -
my question in many ways similar one: pass custom comparator through function, tried solution proposed there, , not work.
in nutshell, have method keeps several sorted structures , performs several comparisons, on elements of type int
, all using same ordering. ordering determined when method called.
the intended call like: myfunction(std::greater<int>());
first, tried declaring function as: void myfunction(binary_operator<int, int, bool> order);
but, per this explanation, binary_function
not suited act base class in function calls.
finally, i tried suggestion this answer (and many other sites), suggested using templates. but, still can not code compile.
the minimal non-working example:
template <typename comparator> void myfunction(comparator order){ if (order(1,2)){ // stuff // compiles ok } std::vector <int> vectosort; // ... initialize std::sort(vectosort.begin(), vectosort.end(), order); // works // compiles ok std::multimap <int, int, order > boundary; // starts kicking, screaming , shouthing }
and compile error get:
error: type/value mismatch @ argument 3 in template parameter list ‘template class std::multimap’ error: expected type, got ‘order’
i figured same trick should work both. not. (edit: can see type
/object
problem now)
can please explain happening here , how the multimap
to use ordering passed function argument?
ps: not using boost project.
it should declared follows:
std::multimap <int, int, comparator> boundary(order); ^^^^^^^^^^
as comments say, need provide type
not object
. these docs construct multimap give examples.
Comments
Post a Comment