Have a map with keys consisting of different types in C++

后端 未结 3 1113
别那么骄傲
别那么骄傲 2021-01-21 20:46

I currently am using a map in C++ and have a couple of values that are ints and booleans, though the majority of them are strings. I know in Java I could do something like this:

3条回答
  •  面向向阳花
    2021-01-21 21:32

    You can have several different maps, and hide the lookup in overloaded functions:

    std::map           map_int;
    std::map   map_string;
    
    Object& lookup(int Key)
    { return map_int[Key]; }
    
    Object& lookup(std::string Key)
    { return map_string[Key]; }
    

    Now all your lookups kan be done with lookup(key).

提交回复
热议问题