I currently have two maps that link enum values to strings, one direction to convert file input to internal representation, the other to print (debug) information about that internal representation. I would like to change the map to a vector for generally faster access and conversion. But then this function, which works as it should, won't work:
template<class T, class Y>
const T & map_value( const std::map<Y,T> &map, const Y &key )
{
return (*map.find(key)).second;
}
So I tried to make it into this:
template<class Cont, class T, class Y>
const T & map_value( const Cont &map, const Y &key )
{
return map[key];
}
But then I get no matching function call. I have just had an epiphany writing this question; const map has no member operator[], which makes this a hard thing to do. How should I write map_value so it works as fast as possible for both vector and map?
Contdeclared/defined?Contbeing aconst std::map<some_enum_class, std::string>or the inverse map. I'd like to change the inverse map to astd::vector<std::string>and convert theenum classvalues to index values.map.find()rather thanmap[], the former does not insert new key, value pairs.vector, whereoperator[]is faster.vectoror let thestd::mapcompare and dive down the binary tree. I vote for the method that is simplest, easiest to read and understand over something more complicated that would produce negligible time savings.