c++ - Common algorithm for maps with different comparison functions -
i have common algorithm 2 maps uses find() , operator[] access map. however, elsewhere in code need iterate on these maps , 1 of them needs sorted reverse comparison other. ended using reverse iterator map, profiling shows me huge amount of time wasted on dereferencing reverse iterator. tried following, didn't work:
struct custom { list<double> doubles; int integer = 0; }; typedef map<double, custom> custommap; typedef map<double, custom, std::greater<double>> custommapgreater; custommap a; custommapgreater b; ... void algorithm(bool achosen) { custommap* chosenmap; if (achosen) { chosenmap = &a; } else { chosenmap = &b; // conversion not possible } // algorithm uses chosenmap follows ... }
any ideas on how can work? have feeling can done templates, i'm not proficient generic programming.
the template way like:
template <typename map> void algorithm(map& map) { // ... }
or, in specific case, even
template <typename comp> void algorithm(std::map<double, custom, comp>& map) { // ... }
and then
void algorithmchooser(bool achosen) { if (achosen) { algorithm(a); } else { algorithm(b); } }
Comments
Post a Comment