Common algorithm for maps with different comparison functions

Viewed 64

I have a common algorithm for 2 maps that uses find() and operator[] to access the map. However, elsewhere in the code I need to iterate over these maps and one of them needs to be sorted with a reverse comparison from the other. I ended up using the reverse iterator for that map, but profiling shows me that a huge amount of time is wasted on dereferencing the reverse iterator. I tried to do the following, but it obviously 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 that uses chosenMap follows
    ...
}

Any ideas on how I can get this to work? I have a feeling something can be done with templates, but I'm not very proficient with generic programming.

2 Answers
Related