mercredi 25 janvier 2017

Best way to have std::map where I can define what is returned if there is no key?

I am using a std::map to map some unsigned char machine values into human readable string types, e.g.:

std::map<unsigned char, std::string> DEVICE_TYPES = {
    { 0x00, "Validator" },
    { 0x03, "SMART Hopper" },
    { 0x06, "SMART Payout" },
    { 0x07, "NV11" },
};

I'd like to modify this so that if the key passed is not present, the map will return "Unknown". I want the caller interface to stay the same (i.e. they just pass their char code in and get a string back). What's the best way to do this? I have C++11 on Windows 7 available.

Aucun commentaire:

Enregistrer un commentaire